Skip to content

Commit f4437fc

Browse files
author
Péter Hauszknecht
committed
update docs
1 parent 664740d commit f4437fc

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

docs/store.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ Dispatches a reducer.
4646

4747
#### Arguments
4848

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.
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.
5050

5151
#### Returns
5252

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.
53+
(*ReducerFunction*): The final reducer that is made by applying the middlewares - if they are given. If the store does enhanced with middlewares, the `dispatch` method returns the same reducer that was taken as argument.
5454

5555
#### Example
5656

@@ -66,8 +66,7 @@ console.log(result === increment) // true
6666

6767
#### Notes
6868

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.
69+
Middlewares can modify the given reducer, so that is not guaranteed that the `dispatch` returns the original reducer that was taken as argument.
7170

7271
### `subscribe(listener)`
7372

@@ -99,33 +98,32 @@ store.dispatch(increment) // listener won't be fired
9998

10099
### `addMiddleware(...middlewares)`
101100

102-
Adds middleware(s) to the store.
101+
Enhances the store with the given middleware(s).
103102

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.
103+
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, so the last added middleware will run first.
105104

106105
#### Arguments
107106

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.
107+
1) **...middlewares** (*MiddlewareFunction: Store -> Next -> Reducer -> any*): Middlewares as variadic arguments. Middleware functions take the `store` instance, a `next` function and the previous `reducer`. The middleware can provide a new reducer via the `next` function.
109108

110109
#### Returns
111110

112-
(*Store: this*): Returns the same `Store` instance for chaining.
111+
(*Store: this*): Returns the enhanced `Store` instance for chaining.
113112

114113
#### Example
115114

116115
```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
116+
const logger = store => next => reducer => {
117+
const state = store.getState()
118+
const nextState = reducer(state)
119+
console.log(state, nextState)
120+
return next(_ => nextState)
123121
}
124122

125-
store.addMiddleware(logger)
123+
const store = new Store({ counter: 0 }).addMiddleware(logger)
126124

127125
store.dispatch(state => ({ counter: state.counter + 1 }))
128-
// logger logs { counter: 1 }
126+
// logger logs { counter: 0 } { counter: 1 }
129127
```
130128

131129
## Static members

src/store/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default class Store<State, R = Reducer<State>> implements IStore<State, R
1010

1111
private state: State;
1212
private listeners: Function[] = [];
13-
private middlewares: Function[] = [];
1413

1514
constructor(initialState: State) {
1615
this.state = initialState;
@@ -43,7 +42,4 @@ export default class Store<State, R = Reducer<State>> implements IStore<State, R
4342
});
4443
return this;
4544
};
46-
47-
private applyMiddlewares = (reducer: R): R =>
48-
<R>this.middlewares.reduce((prevReducer, middleware) => middleware(this, prevReducer), reducer);
4945
}

0 commit comments

Comments
 (0)