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
//! # redux - A Rust implementation of Redux. //! //! Redux provides a clean way of managing states in an application. //! It could be user data such as preferences or information about the state of the program. //! //! ## Concepts //! //! In Redux data is immutable. The only way to change it is to take it and create some new data by following a set of rules. //! //! ### State //! //! A state is the form of data Redux manages. Theoretically it can be anything, but for an easy explanation let's take the following example: //! We have a simple counter application. It does nothing more than counting. //! Our state would look the following: //! //! ``` //! #[derive(Default)] //! struct State { //! counter: i8 //! } //! ``` //! //! ### Actions //! //! To change the state we need to dispatch actions. In Rust, they would usually be represented by an enum. //! For the counter, we want to increment and decrement it. //! //! ``` //! enum Action { //! Increment, //! Decrement //! } //! ``` //! //! ### Reducer //! //! To actually change the state (read: create a new one), we need what is called a reducer. //! It is a simple function which takes in the current state plus the action to perform and returns a new state. //! //! ``` //! # struct State { //! # counter: i8 //! # } //! # //! # enum Action { //! # Increment, //! # Decrement //! # } //! # //! fn reducer(state: &State, action: &Action) -> State { //! match action { //! Action::Increment => State { //! counter: state.counter + 1 //! }, //! Action::Decrement => State { //! counter: state.counter - 1 //! } //! } //! } //! ``` //! //! Note how the reducer uses the old data to create a new state. //! //! ### Store //! //! To put it all together, we use a store which keeps track of a state and provides an easy to use API for dispatching actions. //! The store takes the reducer and an initial state. //! //! ``` //! # #[derive(Default)] //! # struct State { //! # counter: i8 //! # } //! # //! # enum Action { //! # Increment, //! # Decrement //! # } //! # //! # fn reducer(state: &State, action: &Action) -> State { //! # match action { //! # Action::Increment => State { //! # counter: state.counter + 1 //! # }, //! # Action::Decrement => State { //! # counter: state.counter - 1 //! # } //! # } //! # } //! # //! // The store needs to be mutable as it will change its inner state when dispatching actions. //! let mut store = redux_rs::Store::new(reducer, State::default()); //! //! // Let it do its highly complex math. //! store.dispatch(Action::Increment); //! store.dispatch(Action::Decrement); //! //! // Print the current count. //! println!("{}", store.state().counter); //! ``` //! //! ### Subscriptions //! //! Sometimes one might want to listen to changes happening. This is where subscriptions come in. //! They are callbacks with the current state that get called whenever an action gets dispatched. //! //! ``` //! # #[derive(Default)] //! # struct State { //! # counter: i8 //! # } //! # //! # enum Action { //! # Increment, //! # Decrement //! # } //! # //! # fn reducer(state: &State, action: &Action) -> State { //! # match action { //! # Action::Increment => State { //! # counter: state.counter + 1 //! # }, //! # Action::Decrement => State { //! # counter: state.counter - 1 //! # } //! # } //! # } //! # //! # let mut store = redux_rs::Store::new(reducer, State::default()); //! # //! store.subscribe(|state: &State| { //! println!("Something changed! Current value: {}", state.counter); //! }); //! ``` #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), feature(alloc))] #[cfg(not(feature = "std"))] extern crate alloc; #[cfg(not(feature = "std"))] use alloc::vec::Vec; #[cfg(feature = "std")] use std::vec::Vec; mod middleware; mod reducer; mod store; mod subscription; pub use middleware::Middleware; pub use reducer::Reducer; #[cfg(not(feature = "devtools"))] pub use store::Store; pub use subscription::Subscription;