Struct gstore::store::Store [−][src]
pub struct Store<A, S> where
A: Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static, { /* fields omitted */ }
A store is a thing that manages mutations for a given state based on actions.
If you know redux: This is a redux store.
Types
- A: The Action of the Store
- S: The State of the Store
Examples
use std::sync::Mutex; use gstore::prelude::*; #[derive(Clone, Eq, PartialEq, Debug)] enum Action { Start, Stop, Increment, Decrement } #[derive(Default, Clone, Eq, PartialEq)] struct State { count: u32, running: bool } impl LifecycleState for State { fn running(&self) -> bool { self.running } } fn main() { let reducer = |action, state: State| { match action { Action::Increment => State { count: state.count + 1, ..state}, Action::Decrement => State { count: state.count - 1, ..state}, Action::Start => State { running: true, ..state}, Action::Stop => State { running: false, ..state}, } }; let logging_middleware = middleware(|store, next, action: Action| { println!("Handling action {:?}", action); next(store, action); }); let store: Store<Action, State> = Store::new(reducer, vec![logging_middleware]); store.dispatch(Action::Increment); let _count = store.select(|s| s.count.clone()); }
Implementations
impl<A, S> Store<A, S> where
A: Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static,
[src]
impl<A, S> Store<A, S> where
A: Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static,
[src]pub fn new<F>(reducer: F, middlewares: Vec<Middleware<A, S>>) -> Self where
F: Fn(A, S) -> S + 'static,
[src]
F: Fn(A, S) -> S + 'static,
Creates a new Store with a root reducer and all middlewares.
Arguments
- reducer: The root reducer which may be combination of multiple reducers. see also combine_reducers
pub fn dispatch(&self, action: A)
[src]
Dispatches the given Action to the store. This leads to the reducers mutating the state. If middlewares are registered the reducers will run wrapped inside the middleware chain.
pub fn select<R, F>(&self, selector: F) -> R where
F: Fn(&S) -> R,
[src]
F: Fn(&S) -> R,
Selects a value from the state by a given closure.
This does not copy the whole state.
pub fn register<U, C>(&self, select: U, call: C) where
U: Fn(&S, &S) -> bool + 'static,
C: Fn(&S) + 'static,
[src]
U: Fn(&S, &S) -> bool + 'static,
C: Fn(&S) + 'static,
Registers the given callback to the store. The callback is called on every state change.
pub fn sender(&self) -> Sender<A>
[src]
Returns a Sender to use in middleware which does asynchronous stuff.
Trait Implementations
Auto Trait Implementations
impl<A, S> !RefUnwindSafe for Store<A, S>
impl<A, S> !RefUnwindSafe for Store<A, S>
impl<A, S> !UnwindSafe for Store<A, S>
impl<A, S> !UnwindSafe for Store<A, S>