Struct gstore::store::Store [−][src]
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]
A: Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static,
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
impl<A: Clone, S: Clone> Clone for Store<A, S> where
A: Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static,
[src]
A: Clone + Eq + 'static,
S: LifecycleState + Clone + Eq + Default + 'static,
Auto Trait Implementations
impl<A, S> !RefUnwindSafe for Store<A, S>
impl<A, S> !Send for Store<A, S>
impl<A, S> !Sync for Store<A, S>
impl<A, S> Unpin for Store<A, S>
impl<A, S> !UnwindSafe for Store<A, S>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,