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]

pub fn new<F>(reducer: F, middlewares: Vec<Middleware<A, S>>) -> Self where
    F: Fn(A, S) -> S + 'static, 
[src]

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]

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]

Registers the given callback to the store. The callback is called on every state change.

pub fn sender(&self) -> Sender<A>[src]

Trait Implementations

impl<A: Clone, S: Clone> Clone for Store<A, S> where
    A: Clone + Eq + 'static,
    S: LifecycleState + Clone + Eq + Default + 'static, 
[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.