1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::dispatcher::Dispatcher;
use crate::reactor::Reactor;
use crate::reducer::Reducer;
use std::mem;

/// A reactive state container.
///
/// The only way to mutate the internal state managed by Store is by
/// [dispatching](trait.Dispatcher.html) actions on it.
/// The associated reactor is notified upon every state transition.
///
/// ## Example
/// ```rust
/// use reducer::*;
/// use std::error::Error;
/// use std::io::{self, Write};
///
/// // The state of your app.
/// struct Calculator(i32);
///
/// // Actions the user can trigger.
/// struct Add(i32);
/// struct Sub(i32);
/// struct Mul(i32);
/// struct Div(i32);
///
/// impl Reducer<Add> for Calculator {
///     fn reduce(&mut self, Add(x): Add) {
///         self.0 += x;
///     }
/// }
///
/// impl Reducer<Sub> for Calculator {
///     fn reduce(&mut self, Sub(x): Sub) {
///         self.0 -= x;
///     }
/// }
///
/// impl Reducer<Mul> for Calculator {
///     fn reduce(&mut self, Mul(x): Mul) {
///         self.0 *= x;
///     }
/// }
///
/// impl Reducer<Div> for Calculator {
///     fn reduce(&mut self, Div(x): Div) {
///         self.0 /= x;
///     }
/// }
///
/// // The user interface.
/// struct Display;
///
/// impl Reactor<Calculator> for Display {
///     type Output = io::Result<()>;
///     fn react(&self, state: &Calculator) -> Self::Output {
///         io::stdout().write_fmt(format_args!("{}\n", state.0))
///     }
/// }
///
/// fn main() -> Result<(), Box<dyn Error>> {
///     let mut store = Store::new(Calculator(0), Display);
///
///     store.dispatch(Add(5))?; // displays "5"
///     store.dispatch(Mul(3))?; // displays "15"
///     store.dispatch(Sub(1))?; // displays "14"
///     store.dispatch(Div(7))?; // displays "2"
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Store<R, S: Reactor<R>> {
    state: R,
    reactor: S,
}

impl<R, S: Reactor<R>> Store<R, S> {
    /// Constructs the Store given the initial state and a reactor.
    pub fn new(state: R, reactor: S) -> Self {
        Self { state, reactor }
    }

    /// Replaces the reactor and returns the previous one.
    pub fn subscribe(&mut self, reactor: impl Into<S>) -> S {
        mem::replace(&mut self.reactor, reactor.into())
    }
}

impl<A, R, S> Dispatcher<A> for Store<R, S>
where
    R: Reducer<A>,
    S: Reactor<R>,
{
    type Output = S::Output;

    /// Updates the state via [`<R as Reducer<A>>::reduce`](trait.Reducer.html#tymethod.reduce) and
    /// notifies the reactor, returning the result of calling
    /// [`<S as Reactor<R>>::react`](trait.Reactor.html#tymethod.react) with a reference to the
    /// new state.
    fn dispatch(&mut self, action: A) -> S::Output {
        self.state.reduce(action);
        self.reactor.react(&self.state)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mock::*;
    use proptest::*;

    #[test]
    fn default() {
        let store = Store::<MockReducer<()>, MockReactor<_>>::default();

        assert_eq!(store.state, MockReducer::default());
        assert_eq!(store.reactor, MockReactor::default());
    }

    proptest! {
        #[test]
        fn new(actions: Vec<u8>) {
            let state = MockReducer::new(actions);
            let reactor = MockReactor::default();
            let store = Store::new(state.clone(), &reactor);

            assert_eq!(store.state, state);
            assert_eq!(store.reactor, &reactor);
        }
    }

    proptest! {
        #[test]
        fn clone(actions: Vec<u8>) {
            let store = Store::new(MockReducer::new(actions), MockReactor::default());
            assert_eq!(store, store.clone());
        }
    }

    proptest! {
        #[test]
        fn subscribe(actions: Vec<u8>) {
            let state = MockReducer::new(actions);
            let mut store = Store::new(state.clone(), Some(MockReactor::default()));

            assert_eq!(store.state, state);
            assert_eq!(store.reactor, Some(MockReactor::default()));

            assert_eq!(store.subscribe(None), Some(MockReactor::default()));

            assert_eq!(store.state, state);
            assert_eq!(store.reactor, None);

            assert_eq!(store.subscribe(MockReactor::default()), None);

            assert_eq!(store.state, state);
            assert_eq!(store.reactor, Some(MockReactor::default()));
        }
    }

    proptest! {
        #[test]
        fn dispatch(actions: Vec<u8>) {
            let mut store = Store::<MockReducer<_>, MockReactor<_>>::default();

            for (i, &action) in actions.iter().enumerate() {
                assert_eq!(
                    store.dispatch(action),
                    MockReducer::new(actions[0..=i].into())
                );
            }
        }
    }
}