pub trait DataReducer {
type Channel;
type Action;
// Required method
fn reduce(
&mut self,
action: Self::Action,
) -> ChannelSelection<Self::Channel>;
}Expand description
Trait for implementing a reducer pattern on your state. Reducers allow you to define actions that modify the state in a controlled way.
Implement this trait on your state type to enable the RadioReducer functionality.
§Example
ⓘ
#[derive(Clone)]
struct Counter {
count: i32,
}
#[derive(Clone)]
enum CounterAction {
Increment,
Decrement,
Set(i32),
}
impl DataReducer for Counter {
type Channel = CounterChannel;
type Action = CounterAction;
fn reduce(&mut self, action: Self::Action) -> ChannelSelection<Self::Channel> {
match action {
CounterAction::Increment => self.count += 1,
CounterAction::Decrement => self.count -= 1,
CounterAction::Set(value) => self.count = value,
}
ChannelSelection::Current
}
}