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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
use std::rc::Rc; /// A wrapper for a function that implements the [Reducer](Reducer) /// trait. /// /// ## Example /// /// ``` /// # #[derive(Clone)] /// # struct MyState { /// # pub variable: bool /// # } /// # /// # enum MyAction { /// # SomeAction /// # } /// # /// # enum MyEvent { /// # SomeEvent /// # } /// # /// # enum MyEffect { /// # SomeEffect /// # } /// use reactive_state::{ReducerFn, ReducerResult, Reducer}; /// use std::rc::Rc; /// /// let reducer: ReducerFn<MyState, MyAction, MyEvent, MyEffect> = |state, action| { /// let new_state = match action { /// MyAction::SomeAction => { /// // create a new state to mutate and return /// let mut new_state = MyState::clone(state); /// new_state.variable = true; /// Rc::new(new_state) /// } /// }; /// /// ReducerResult { /// state: new_state, /// events: vec![], /// effects: vec![], /// } /// }; /// /// let state1 = Rc::new(MyState { /// variable: false /// }); /// /// let result = reducer.reduce(&state1, &MyAction::SomeAction); /// let state2 = &result.state; /// /// assert_eq!(false, state1.variable); /// assert_eq!(true, state2.variable); /// ``` /// /// For a more comprehensive example of how reducers are used in the /// context of the entire system, see [reactive_state](crate). pub type ReducerFn<State, Action, Event, Effect> = fn(&Rc<State>, &Action) -> ReducerResult<State, Event, Effect>; impl<State, Action, Event, Effect> Reducer<State, Action, Event, Effect> for ReducerFn<State, Action, Event, Effect> { fn reduce( &self, prev_state: &Rc<State>, action: &Action, ) -> ReducerResult<State, Event, Effect> { (self)(prev_state, action) } } /// Using the [reduce()](Reducer::reduce()) method, implementors of /// this trait take an `Action` submitted to a store via /// [Store::dispatch()](crate::Store::dispatch()) and modifies the /// `State` in the store, producing a new `State`, and also producing /// events and effects associated with the `Action` and state /// modifications that occurred. /// /// For an example of how a reducer function should work, see /// [ReducerFn](ReducerFn). For an example of how to use one in /// conjunction with a [Store](crate::Store), see /// [reactive_state](crate). pub trait Reducer<State, Action, Event, Effect> { /// Take an `Action` submitted to a store via /// [Store::dispatch()](crate::Store::dispatch()) and modifies the /// `prev_state`, producing a new `State`, and also producing /// events associated with the `Action` and state modifications /// that occurred. /// /// This method should be a pure function, with any required side /// effects being emmitted via the returned /// [ReducerResult](ReducerResult). /// /// `Events`s should generally be treated purely as a notification /// that some subset of the state has been modified, such that /// playing the events and state transitions in reverse will /// result in the same application behaviour. /// /// If no `Event`s are returned then it is assumed that the state /// has not changed, and store listeners do not need to be /// notified. /// /// `Effect`s are side effects invoked as a result of the action, /// these may involve dispatching further actions, or modifying /// some other part of the system that the store is involved with. /// `Effect`s are processed using /// [Middleware](crate::middleware::Middleware) which has been /// added to the [Store](crate::Store). fn reduce( &self, prev_state: &Rc<State>, action: &Action, ) -> ReducerResult<State, Event, Effect>; } /// The result of a [Reducer::reduce()] function. /// /// `Events`s should generally be treated purely as a notification /// that some subset of the state has been modified, such that /// playing the events and state transitions in reverse will /// result in the same application behaviour. /// /// `Effect`s are side effects invoked as a result of the action, /// these may involve dispatching further actions, or modifying /// some other part of the system that the store is involved with. /// `Effect`s are processed using [Middleware](crate::middleware::Middleware) /// which has been added to the [Store](crate::Store). pub struct ReducerResult<State, Event, Effect> { pub state: Rc<State>, pub events: Vec<Event>, pub effects: Vec<Effect>, } impl<State, Event, Effect> Default for ReducerResult<State, Event, Effect> where State: Default, { fn default() -> Self { Self { state: Rc::new(State::default()), events: vec![], effects: vec![], } } } // TODO: create a zero cost macro version of this #17 /// A [Reducer] composed of multiple reducers. pub struct CompositeReducer<State, Action, Event, Effect> { reducers: Vec<Box<dyn Reducer<State, Action, Event, Effect>>>, } impl<State, Action, Event, Effect> CompositeReducer<State, Action, Event, Effect> { /// Create a new [CompositeReducer]. pub fn new(reducers: Vec<Box<dyn Reducer<State, Action, Event, Effect>>>) -> Self { CompositeReducer { reducers } } } impl<State, Action, Event, Effect> Reducer<State, Action, Event, Effect> for CompositeReducer<State, Action, Event, Effect> { fn reduce( &self, prev_state: &Rc<State>, action: &Action, ) -> ReducerResult<State, Event, Effect> { let mut sum_result: ReducerResult<State, Event, Effect> = ReducerResult { state: prev_state.clone(), events: Vec::new(), effects: Vec::new(), }; for reducer in &self.reducers { let result = reducer.reduce(&sum_result.state, action); sum_result.state = result.state; sum_result.events.extend(result.events); sum_result.effects.extend(result.effects); } sum_result } } #[cfg(test)] mod tests { use crate::{CompositeReducer, Reducer, ReducerResult}; use std::rc::Rc; struct TestState { emitted_events: Vec<TestEvent>, } impl Default for TestState { fn default() -> Self { TestState { emitted_events: Vec::new(), } } } struct TestAction; #[derive(Debug, Clone, PartialEq)] enum TestEvent { Event1, Event2, } #[derive(Debug, PartialEq)] enum TestEffect { Effect1, Effect2, } struct Reducer1; impl Reducer<TestState, TestAction, TestEvent, TestEffect> for Reducer1 { fn reduce( &self, prev_state: &Rc<TestState>, _action: &TestAction, ) -> crate::ReducerResult<TestState, TestEvent, TestEffect> { let mut emitted_events = prev_state.emitted_events.clone(); emitted_events.push(TestEvent::Event1); let state = Rc::new(TestState { emitted_events }); ReducerResult { state, events: vec![TestEvent::Event1], effects: vec![TestEffect::Effect1], } } } struct Reducer2; impl Reducer<TestState, TestAction, TestEvent, TestEffect> for Reducer2 { fn reduce( &self, prev_state: &Rc<TestState>, _action: &TestAction, ) -> crate::ReducerResult<TestState, TestEvent, TestEffect> { let mut emitted_events = prev_state.emitted_events.clone(); emitted_events.push(TestEvent::Event2); let state = Rc::new(TestState { emitted_events }); ReducerResult { state, events: vec![TestEvent::Event2], effects: vec![TestEffect::Effect2], } } } #[test] fn composite_reducer() { let reducer = CompositeReducer::new(vec![Box::new(Reducer1), Box::new(Reducer2)]); let result = reducer.reduce(&Rc::new(TestState::default()), &TestAction); assert_eq!( result.state.emitted_events, vec![TestEvent::Event1, TestEvent::Event2] ); assert_eq!(result.events, vec![TestEvent::Event1, TestEvent::Event2]); assert_eq!( result.effects, vec![TestEffect::Effect1, TestEffect::Effect2] ); } }