|
1 | 1 | import { Reducers, State } from '../default'; |
2 | 2 | import Callback from '../types/callback'; |
| 3 | +import DispatchFunction from '../types/dispatch-function'; |
3 | 4 | import Dispatcher, { ExtractArguments } from '../types/dispatcher'; |
4 | 5 | import Dispatchers from '../types/dispatchers'; |
5 | 6 | import Middleware, { MiddlewareCreator } from '../types/middleware'; |
@@ -135,14 +136,25 @@ export default class GlobalStateManager< |
135 | 136 | ): Dispatcher<G, A> { |
136 | 137 | return (...args: A): Promise<G> => { |
137 | 138 | return this.set( |
138 | | - reducer(this.state, this.dispatchers, ...args), |
139 | | - name, |
| 139 | + reducer(this.state, this.dispatcherMap, ...args), |
| 140 | + name, |
140 | 141 | args, |
141 | 142 | ) |
142 | 143 | .then((): G => this.state); |
143 | 144 | }; |
144 | 145 | } |
145 | 146 |
|
| 147 | + // The dispatcher map, contrary to this.dispatchers, is the map that belongs |
| 148 | + // *to* a dispatcher. In addition to the map *of* dispatchers, it includes |
| 149 | + // a dispatch function that sets the global state. |
| 150 | + public get dispatcherMap(): DispatchFunction<G> & Dispatchers<G, R> { |
| 151 | + const dispatch: DispatchFunction<G> = (newGlobalState: NewGlobalState<G>): Promise<G> => |
| 152 | + this.set(newGlobalState).then((): G => this.state); |
| 153 | + return Object.assign(dispatch, this.dispatchers); |
| 154 | + } |
| 155 | + |
| 156 | + // The dispatchers are a map *of* dispatchers. This is in contrast to |
| 157 | + // this.dispatcherMap, which is a map that belongs *to* a dispatcher. |
146 | 158 | public get dispatchers(): Dispatchers<G, R> { |
147 | 159 | return copyObject(this._dispatchers); |
148 | 160 | } |
|
0 commit comments