pub struct Store<S, A: Action, E = NoEffect> { /* private fields */ }Expand description
Centralized state store with Redux-like reducer pattern
The store holds the application state and provides a single point
for state mutations through the dispatch method.
§Type Parameters
S- The application state typeA- The action type (must implementAction)
§Example
use tui_dispatch_core::{Action, ReducerResult, Store};
#[derive(Clone, Debug)]
enum MyAction { Increment, Decrement }
impl Action for MyAction {
fn name(&self) -> &'static str {
match self {
MyAction::Increment => "Increment",
MyAction::Decrement => "Decrement",
}
}
}
#[derive(Default)]
struct AppState { counter: i32 }
fn reducer(state: &mut AppState, action: MyAction) -> ReducerResult {
match action {
MyAction::Increment => { state.counter += 1; ReducerResult::changed() }
MyAction::Decrement => { state.counter -= 1; ReducerResult::changed() }
}
}
let mut store = Store::new(AppState::default(), reducer);
let result = store.dispatch(MyAction::Increment);
assert!(result.changed);
assert_eq!(store.state().counter, 1);Implementations§
Source§impl<S, A: Action, E> Store<S, A, E>
impl<S, A: Action, E> Store<S, A, E>
Trait Implementations§
Source§impl<S, A: Action, E> RuntimeStore<S, A, E> for Store<S, A, E>
impl<S, A: Action, E> RuntimeStore<S, A, E> for Store<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 Store<S, A, E>where
S: Freeze,
impl<S, A, E> RefUnwindSafe for Store<S, A, E>
impl<S, A, E> Send for Store<S, A, E>
impl<S, A, E> Sync for Store<S, A, E>
impl<S, A, E> Unpin for Store<S, A, E>
impl<S, A, E> UnsafeUnpin for Store<S, A, E>where
S: UnsafeUnpin,
impl<S, A, E> UnwindSafe for Store<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