|
| 1 | +# Store |
| 2 | + |
| 3 | +The `Store` holds the whole state tree of your application. You need to instantiate it. |
| 4 | + |
| 5 | +## Constructor |
| 6 | + |
| 7 | +#### Arguments |
| 8 | + |
| 9 | +1) **initialState** (*any*): The initial state of your application. Usually it is an object that represents the state tree. |
| 10 | + |
| 11 | +#### Returns |
| 12 | + |
| 13 | +(*Store*): The new `Store` instance. |
| 14 | + |
| 15 | +#### Example |
| 16 | + |
| 17 | +```javascript |
| 18 | +import Store from 'repatch' |
| 19 | + |
| 20 | +const store = new Store({ counter: 0 }) |
| 21 | +``` |
| 22 | + |
| 23 | +## Methods |
| 24 | + |
| 25 | +### `getState()` |
| 26 | + |
| 27 | +This method returns the current state of the store. |
| 28 | + |
| 29 | +#### Returns |
| 30 | + |
| 31 | +(*any*): The current state of the store. |
| 32 | + |
| 33 | +#### Example |
| 34 | + |
| 35 | +```javascript |
| 36 | +const store = new Store({ counter: 0 }) |
| 37 | + |
| 38 | +const state = store.getState() |
| 39 | + |
| 40 | +console.log(state.counter === 0) // true |
| 41 | +``` |
| 42 | + |
| 43 | +### `dispatch(reducer)` |
| 44 | + |
| 45 | +Dispatches a reducer. |
| 46 | + |
| 47 | +#### Arguments |
| 48 | + |
| 49 | +1) **reducer** (*ReducerFunction: State -> State*): That reducer will reduce the state of the store. This takes the current state and returns the next state. The reducer will be run synchronously after applying the middlewares - if they are given. |
| 50 | + |
| 51 | +#### Returns |
| 52 | + |
| 53 | +(*ReducerFunction*): The final reducer that is made by applying the middlewares - if they are given. If the store does not have middlewares, the `dispatch` method returns the same reducer that was taken as argument. |
| 54 | + |
| 55 | +#### Example |
| 56 | + |
| 57 | +```javascript |
| 58 | +const store = new Store({ counter: 0 }) |
| 59 | + |
| 60 | +const increment = state => ({ counter: state.counter + 1 }) |
| 61 | + |
| 62 | +const result = store.dispatch(increment) |
| 63 | + |
| 64 | +console.log(result === increment) // true |
| 65 | +``` |
| 66 | + |
| 67 | +#### Notes |
| 68 | + |
| 69 | +1) Middlewares can modify the given reducer, so that is not guaranteed that the `dispatch` returns the original reducer that was taken as argument. |
| 70 | +2) The `dispatch` only runs the reducer, when the final reducer (after applying middlewares) is still a function. If the final reducer is a function, then the dispatch modifies the state by its returned value. This behaviour is strongly used by the [thunk](thunk.md) middleware, that returns the returned value of the delegate. |
| 71 | + |
| 72 | +### `subscribe(listener)` |
| 73 | + |
| 74 | +Adds a state change listener. |
| 75 | + |
| 76 | +#### Arguments |
| 77 | + |
| 78 | +1) **listener** (*ListenerFunction: void -> void*): The listener that will be synchronously run after the state was modified by dispatching a reducer. |
| 79 | + |
| 80 | +#### Returns |
| 81 | + |
| 82 | +(*UnsubscribeFunction: void -> void*): The unsubscribe function, that you can use to unsubscribe the given listener. |
| 83 | + |
| 84 | +#### Example |
| 85 | + |
| 86 | +```javascript |
| 87 | +const store = new Store({ counter: 0 }) |
| 88 | + |
| 89 | +const increment = state => ({ counter: state.counter + 1 }) |
| 90 | + |
| 91 | +const unsubscribe = store.subscribe(() => console.log(store.getState())) |
| 92 | + |
| 93 | +store.dispatch(increment) // listener logs the new state |
| 94 | + |
| 95 | +unsubscribe() |
| 96 | + |
| 97 | +store.dispatch(increment) // listener won't be fired |
| 98 | +``` |
| 99 | + |
| 100 | +### `addMiddleware(...middlewares)` |
| 101 | + |
| 102 | +Adds middleware(s) to the store. |
| 103 | + |
| 104 | +Middlewares will be run at dispatching before the store applies the new state of the reducer. The added middlewares are composed by order of addition. |
| 105 | + |
| 106 | +#### Arguments |
| 107 | + |
| 108 | +1) **...middlewares** (*MiddlewareFunction: (Store, Reducer) -> Reducer*): Middlewares as variadic arguments. Middleware functions take the `Store` instance and the dispatched reducer and return a new reducer. |
| 109 | + |
| 110 | +#### Returns |
| 111 | + |
| 112 | +(*Store: this*): Returns the same `Store` instance for chaining. |
| 113 | + |
| 114 | +#### Example |
| 115 | + |
| 116 | +```javascript |
| 117 | +const store = new Store({ counter: 0 }) |
| 118 | + |
| 119 | +const logger = (store, reducer) => { |
| 120 | + const nextState = reducer(store.getState()) |
| 121 | + console.log(nextState) |
| 122 | + return _ => nextState |
| 123 | +} |
| 124 | + |
| 125 | +store.addMiddleware(logger) |
| 126 | + |
| 127 | +store.dispatch(state => ({ counter: state.counter + 1 })) |
| 128 | +// logger logs { counter: 1 } |
| 129 | +``` |
| 130 | + |
| 131 | +## Static members |
| 132 | + |
| 133 | +### `thunk` |
| 134 | + |
| 135 | +The [thunk](thunk.md) middleware. |
0 commit comments