simple/
simple.rs

1use rs_store::{DispatchOp, FnReducer, FnSubscriber, StoreBuilder};
2use std::sync::Arc;
3
4pub fn main() {
5    // new store with reducer
6    let store = StoreBuilder::new(0)
7        .with_reducer(Box::new(FnReducer::from(|state: &i32, action: &i32| {
8            println!("reducer: {} + {}", state, action);
9            DispatchOp::Dispatch(state + action, vec![])
10        })))
11        .build()
12        .unwrap();
13
14    // add subscriber
15    store
16        .add_subscriber(Arc::new(FnSubscriber::from(
17            |state: &i32, _action: &i32| {
18                println!("subscriber: state: {}", state);
19            },
20        )))
21        .unwrap();
22
23    // dispatch actions
24    store.dispatch(41).expect("no error");
25    store.dispatch(1).expect("no error");
26
27    // stop the store
28    match store.stop() {
29        Ok(_) => println!("store stopped"),
30        Err(e) => {
31            panic!("store stop failed  : {:?}", e);
32        }
33    }
34
35    assert_eq!(store.get_state(), 42);
36}