[−][src]Struct gstore::Store
The store is a thing that manages mutations for a given state based on actions.
A State can be any kind of data structure an application is based on. For a 'counter' app it
might be a struct with a single u32 field.
Actions represent the possible features of the app affecting the state. In the example above
this might be an enum with the values Increment
and Decrement
.
A Store works with two threads: The main UI thread and a background thread. TODO explain.
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::{ combine_reducers, Store }; #[derive(Debug, Clone)] struct State { count: u32, } #[derive(Debug, Clone, Eq, PartialEq)] enum Action { Increment, Decrement, Shutdown, } 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 => Some(State { count: state.count + 1, }), Action::Decrement => Some(State { count: state.count - 1, }), Action::Shutdown => None, }); 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: Send + Clone + Eq + PartialEq + 'static,
S: Send + Clone + 'static,
[src]
A: Send + Clone + Eq + PartialEq + 'static,
S: Send + Clone + 'static,
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]
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]
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, 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>,