pub struct EffectStore<S, A, E> { /* private fields */ }Expand description
A store that supports effect-emitting reducers.
Similar to Store, but the reducer returns
DispatchResult<E> instead of bool, allowing it to declare
side effects alongside state changes.
§Example
ⓘ
use tui_dispatch::{DispatchResult, EffectStore};
enum Effect { Log(String) }
struct State { count: i32 }
enum Action { Increment }
fn reducer(state: &mut State, action: Action) -> DispatchResult<Effect> {
match action {
Action::Increment => {
state.count += 1;
DispatchResult::changed_with(Effect::Log(format!("count is {}", state.count)))
}
}
}
let mut store = EffectStore::new(State { count: 0 }, reducer);
let result = store.dispatch(Action::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) -> DispatchResult<E>
pub fn dispatch(&mut self, action: A) -> DispatchResult<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.
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> 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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
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