pub struct EffectStore<S, A, E> { /* private fields */ }Expand description
A store that supports effect-emitting reducers.
Similar to Store, but the reducer returns
ReducerResult<E> instead of bool, allowing it to declare
side effects alongside state changes.
§Example
use tui_dispatch_core::{Action, ReducerResult, EffectStore};
#[derive(Clone, Debug)]
enum MyAction { Increment }
impl Action for MyAction {
fn name(&self) -> &'static str { "Increment" }
}
enum Effect { Log(String) }
struct State { count: i32 }
fn reducer(state: &mut State, action: MyAction) -> ReducerResult<Effect> {
match action {
MyAction::Increment => {
state.count += 1;
ReducerResult::changed_with(Effect::Log(format!("count is {}", state.count)))
}
}
}
let mut store = EffectStore::new(State { count: 0 }, reducer);
let result = store.dispatch(MyAction::Increment);
assert!(result.changed);
assert_eq!(result.effects.len(), 1);Implementations§
Source§impl<S, A, E> EffectStore<S, A, E>where
A: Action,
impl<S, A, E> EffectStore<S, A, E>where
A: Action,
Sourcepub fn new(state: S, reducer: EffectReducer<S, A, E>) -> Self
pub fn new(state: S, reducer: EffectReducer<S, A, E>) -> Self
Create a new effect store with the given initial state and reducer.
Sourcepub fn state_mut(&mut self) -> &mut S
pub fn state_mut(&mut self) -> &mut S
Get a mutable reference to the state.
Use sparingly - prefer dispatching actions for state changes. This is mainly useful for initialization.
Sourcepub fn dispatch(&mut self, action: A) -> ReducerResult<E>
pub fn dispatch(&mut self, action: A) -> ReducerResult<E>
Dispatch an action to the store.
The reducer is called with the current state and action, returning whether state changed and any effects to process.
Trait Implementations§
Source§impl<S, A: Action, E> EffectStoreLike<S, A, E> for EffectStore<S, A, E>
impl<S, A: Action, E> EffectStoreLike<S, A, E> for EffectStore<S, A, E>
Source§fn dispatch(&mut self, action: A) -> ReducerResult<E>
fn dispatch(&mut self, action: A) -> ReducerResult<E>
Dispatch an action and return state changes plus effects.
Source§fn try_dispatch(&mut self, action: A) -> Result<ReducerResult<E>, DispatchError>
fn try_dispatch(&mut self, action: A) -> Result<ReducerResult<E>, DispatchError>
Dispatch an action and return state changes plus effects. Read more
Auto Trait Implementations§
impl<S, A, E> Freeze for EffectStore<S, A, E>where
S: Freeze,
impl<S, A, E> RefUnwindSafe for EffectStore<S, A, E>
impl<S, A, E> Send for EffectStore<S, A, E>
impl<S, A, E> Sync for EffectStore<S, A, E>
impl<S, A, E> Unpin for EffectStore<S, A, E>
impl<S, A, E> UnsafeUnpin for EffectStore<S, A, E>where
S: UnsafeUnpin,
impl<S, A, E> UnwindSafe for EffectStore<S, A, E>
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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