pub struct Radio<Value, Channel>where
Channel: RadioChannel<Value>,
Value: 'static,{ /* private fields */ }Expand description
A reactive handle to the global state for a specific channel.
Radio provides methods to read and write the global state, and automatically subscribes
the current component to re-render when the associated channel is notified.
Each Radio instance is tied to a specific channel, allowing fine-grained control
over which components update when the state changes.
§Examples
§Basic usage
#[derive(PartialEq)]
struct MyComponent {}
impl Component for MyComponent {
fn render(&self) -> impl IntoElement {
let mut radio = use_radio(MyChannel::Count);
rect()
.child(label().text(format!("Count: {}", radio.read().count)))
.child(
Button::new()
.on_press(move |_| radio.write().count += 1)
.child("Increment"),
)
}
}§Using reducers
#[derive(Clone)]
struct CounterState {
count: i32,
}
impl DataReducer for CounterState {
type Channel = CounterChannel;
type Action = CounterAction;
fn reduce(&mut self, action: CounterAction) -> ChannelSelection<CounterChannel> {
match action {
CounterAction::Increment => self.count += 1,
CounterAction::Decrement => self.count -= 1,
}
ChannelSelection::Current
}
}
#[derive(PartialEq)]
struct CounterComponent {}
impl Component for CounterComponent {
fn render(&self) -> impl IntoElement {
let mut radio = use_radio(CounterChannel::Count);
rect()
.child(
Button::new()
.on_press(move |_| radio.apply(CounterAction::Increment))
.child("+"),
)
.child(label().text(format!("{}", radio.read().count)))
.child(
Button::new()
.on_press(move |_| radio.apply(CounterAction::Decrement))
.child("-"),
)
}
}Implementations§
Source§impl<Value, Channel> Radio<Value, Channel>where
Channel: RadioChannel<Value>,
impl<Value, Channel> Radio<Value, Channel>where
Channel: RadioChannel<Value>,
Sourcepub fn with(&self, cb: impl FnOnce(ReadRef<'_, Value>))
pub fn with(&self, cb: impl FnOnce(ReadRef<'_, Value>))
Read the current state value inside a callback.
Example:
radio.with(|value| {
// Do something with `value`
});Sourcepub fn write(&mut self) -> RadioGuard<Value, Channel>
pub fn write(&mut self) -> RadioGuard<Value, Channel>
Get a mutable reference to the state for writing. Changes will notify subscribers to this radio’s channel.
Returns a RadioGuard that allows direct mutation of the state.
§Example
radio.write().count += 1;Sourcepub fn write_with(&mut self, cb: impl FnOnce(RadioGuard<Value, Channel>))
pub fn write_with(&mut self, cb: impl FnOnce(RadioGuard<Value, Channel>))
Get a mutable reference to the current state value, inside a callback.
Example:
radio.write_with(|value| {
// Modify `value`
});Sourcepub fn write_channel(&mut self, channel: Channel) -> RadioGuard<Value, Channel>
pub fn write_channel(&mut self, channel: Channel) -> RadioGuard<Value, Channel>
Sourcepub fn write_channel_with(
&mut self,
channel: Channel,
cb: impl FnOnce(RadioGuard<Value, Channel>),
)
pub fn write_channel_with( &mut self, channel: Channel, cb: impl FnOnce(RadioGuard<Value, Channel>), )
Get a mutable reference to the current state value, inside a callback.
Example:
radio.write_channel_with(Channel::Whatever, |value| {
// Modify `value`
});Sourcepub fn write_with_channel_selection(
&mut self,
cb: impl FnOnce(&mut Value) -> ChannelSelection<Channel>,
) -> ChannelSelection<Channel>
pub fn write_with_channel_selection( &mut self, cb: impl FnOnce(&mut Value) -> ChannelSelection<Channel>, ) -> ChannelSelection<Channel>
Get a mutable reference to the current state value, inside a callback that returns the channel to be used.
Example:
radio.write_with_channel_selection(|value| {
// Modify `value`
if value.cool {
ChannelSelection::Select(Channel::Whatever)
} else {
ChannelSelection::Silence
}
});Sourcepub fn write_silently(&mut self) -> RadioGuard<Value, Channel>
pub fn write_silently(&mut self) -> RadioGuard<Value, Channel>
Modify the state silently, no component will be notified.
This is not recommended, the only intended usage for this is inside RadioAsyncReducer.
Trait Implementations§
Source§impl<Value, Channel> Clone for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
impl<Value, Channel> Clone for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
Source§impl<Value, Channel> MutView<'static, Value> for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
impl<Value, Channel> MutView<'static, Value> for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
Source§impl<Value, Channel> PartialEq for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
impl<Value, Channel> PartialEq for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
Source§impl<Data: DataAsyncReducer<Channel = Channel, Action = Action>, Channel: RadioChannel<Data>, Action> RadioAsyncReducer for Radio<Data, Channel>
impl<Data: DataAsyncReducer<Channel = Channel, Action = Action>, Channel: RadioChannel<Data>, Action> RadioAsyncReducer for Radio<Data, Channel>
Source§impl<Data: DataReducer<Channel = Channel, Action = Action>, Channel: RadioChannel<Data>, Action> RadioReducer for Radio<Data, Channel>
impl<Data: DataReducer<Channel = Channel, Action = Action>, Channel: RadioChannel<Data>, Action> RadioReducer for Radio<Data, Channel>
impl<Value, Channel> Copy for Radio<Value, Channel>where
Channel: RadioChannel<Value>,
Auto Trait Implementations§
impl<Value, Channel> Freeze for Radio<Value, Channel>
impl<Value, Channel> !RefUnwindSafe for Radio<Value, Channel>
impl<Value, Channel> !Send for Radio<Value, Channel>
impl<Value, Channel> !Sync for Radio<Value, Channel>
impl<Value, Channel> Unpin for Radio<Value, Channel>
impl<Value, Channel> !UnwindSafe for Radio<Value, Channel>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> ComponentProps for T
impl<T> ComponentProps for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more