[][src]Struct gstore::store::Store

pub struct Store<A, S> where
    A: LifecycleAction + Send + Clone + Eq + PartialEq + 'static,
    S: Send + Clone + 'static, 
{ /* fields omitted */ }

A store is a thing that manages mutations for a given state based on actions.

If you know redux: This is like redux but without middleware (yet).

Types

A: The Action of the Store S: The State of the Store

Examples


#[macro_use]
extern crate lazy_static;


use std::rc::Rc;
use std::sync::Mutex;

use gstore::store::{ combine_reducers, Store, LifecycleAction };


#[derive(Debug, Clone)]
struct State {
   count: u32,
}

#[derive(Debug, Clone, Eq, PartialEq)]
enum Action {
    Increment,
    Decrement,
    Startup,
    Shutdown,
}

impl LifecycleAction for Action {
    fn is_stop(&self) -> bool {
        self == &Action::Shutdown
    }
    fn is_start(&self) -> bool {
        self == &Action::Startup
    }
}

lazy_static! {
    static ref STATE: Mutex<State> = Mutex::new(State { count: 0 });
}

fn main() {
    let mutex = Mutex::new(State { count: 0 });
    let store: Rc<Store<Action, State>> = Rc::new(Store::new(&STATE));
    let join = combine_reducers(store.clone(), &STATE, |action, state| match action {
        Action::Increment => State {
            count: state.count + 1,
        },
        Action::Decrement => State {
         count: state.count - 1,
        },
        _ => state,
    });
    store.send(Action::Increment);
    store.send(Action::Increment);
    store.send(Action::Increment);
    store.send(Action::Decrement);
    store.send(Action::Shutdown);
    join.join().unwrap().expect("Error during Store handling.");
    assert_eq!(STATE.lock().unwrap().count, 2);
}

Implementations

impl<A, S> Store<A, S> where
    A: LifecycleAction + Send + Clone + Eq + PartialEq + 'static,
    S: Send + Clone + 'static, 
[src]

pub fn new(state: &'static Mutex<S>) -> Self[src]

Creates a new state.

Creates a new state with the reference to the static State mutex.

pub fn send(&self, action: A)[src]

Sends the given Action to the store.

This will trigger the reducers as well as notify all receivers (after the reducers) to handle the state change.

pub fn receive(&self, callback: impl Fn(A, &S) + 'static)[src]

Registers the callback in the store.

Arguments

callback: A closure which receives a sent Action and the State after the reducers were applied.

pub fn ui_send(&self, action: A)[src]

Send the given action from the ui thread.

May only be used in the connected UI thread handler.

pub fn ui_try_receive(&self) -> Result<A, TryRecvError>[src]

Tries to receive an Action from the Store.

May only be used in the connected UI thread handler.

Auto Trait Implementations

impl<A, S> !RefUnwindSafe for Store<A, S>[src]

impl<A, S> !Send for Store<A, S>[src]

impl<A, S> !Sync for Store<A, S>[src]

impl<A, S> Unpin for Store<A, S>[src]

impl<A, S> !UnwindSafe for Store<A, S>[src]

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, 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.