simple/
simple.rs

1use rs_store::{DispatchOp, Dispatcher, 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, None)
10        })))
11        .build()
12        .unwrap();
13
14    // add subscriber
15    store.add_subscriber(Arc::new(FnSubscriber::from(
16        |state: &i32, _action: &i32| {
17            println!("subscriber: state: {}", state);
18        },
19    )));
20
21    // dispatch actions
22    store.dispatch(41).expect("no error");
23    store.dispatch(1).expect("no error");
24
25    // stop the store
26    store.stop();
27
28    assert_eq!(store.get_state(), 42);
29}