redux_rs/
subscriber.rs

1/// # Subscriber trait
2/// A subscriber is what gets called every time a new state is calculated.
3/// You create a subscriber by implementing the `Subscriber` trait or by creating a function with the signature `Fn(&State)`
4///
5/// ## Trait example
6/// ```
7/// use redux_rs::Subscriber;
8///
9/// #[derive(Debug)]
10/// struct Counter(i8);
11///
12/// struct PrintSubscriber;
13/// impl Subscriber<Counter> for PrintSubscriber {
14///     fn notify(&self, state: &Counter) {
15///         println!("State changed: {:?}", state);
16///     }
17/// }
18/// ```
19///
20/// ## Fn example
21/// ```
22/// use redux_rs::{Store, Subscriber};
23///
24/// #[derive(Debug)]
25/// struct Counter(i8);
26///
27/// fn print_subscriber(state: &Counter) {
28///     println!("State changed: {:?}", state);
29/// }
30///
31/// # #[tokio::main(flavor = "current_thread")]
32/// # async fn async_test() {
33/// # let store = Store::new_with_state(|store: Counter, _action: ()| store, Counter(0));
34/// # store.subscribe(print_subscriber).await;
35/// # }
36/// ```
37pub trait Subscriber<State> {
38    fn notify(&self, state: &State);
39}
40
41impl<F, State> Subscriber<State> for F
42where
43    F: Fn(&State),
44{
45    fn notify(&self, state: &State) {
46        self(state);
47    }
48}