1 |
3a515b92
|
cagy
|
/// <reference types="symbol-observable" />
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* An *action* is a plain object that represents an intention to change the
|
5 |
|
|
* state. Actions are the only way to get data into the store. Any data,
|
6 |
|
|
* whether from UI events, network callbacks, or other sources such as
|
7 |
|
|
* WebSockets needs to eventually be dispatched as actions.
|
8 |
|
|
*
|
9 |
|
|
* Actions must have a `type` field that indicates the type of action being
|
10 |
|
|
* performed. Types can be defined as constants and imported from another
|
11 |
|
|
* module. It's better to use strings for `type` than Symbols because strings
|
12 |
|
|
* are serializable.
|
13 |
|
|
*
|
14 |
|
|
* Other than `type`, the structure of an action object is really up to you.
|
15 |
|
|
* If you're interested, check out Flux Standard Action for recommendations on
|
16 |
|
|
* how actions should be constructed.
|
17 |
|
|
*
|
18 |
|
|
* @template T the type of the action's `type` tag.
|
19 |
|
|
*/
|
20 |
|
|
export interface Action<T = any> {
|
21 |
|
|
type: T
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* An Action type which accepts any other properties.
|
26 |
|
|
* This is mainly for the use of the `Reducer` type.
|
27 |
|
|
* This is not part of `Action` itself to prevent types that extend `Action` from
|
28 |
|
|
* having an index signature.
|
29 |
|
|
*/
|
30 |
|
|
export interface AnyAction extends Action {
|
31 |
|
|
// Allows any extra properties to be defined in an action.
|
32 |
|
|
[extraProps: string]: any
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
/**
|
36 |
|
|
* Internal "virtual" symbol used to make the `CombinedState` type unique.
|
37 |
|
|
*/
|
38 |
|
|
declare const $CombinedState: unique symbol
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* State base type for reducers created with `combineReducers()`.
|
42 |
|
|
*
|
43 |
|
|
* This type allows the `createStore()` method to infer which levels of the
|
44 |
|
|
* preloaded state can be partial.
|
45 |
|
|
*
|
46 |
|
|
* Because Typescript is really duck-typed, a type needs to have some
|
47 |
|
|
* identifying property to differentiate it from other types with matching
|
48 |
|
|
* prototypes for type checking purposes. That's why this type has the
|
49 |
|
|
* `$CombinedState` symbol property. Without the property, this type would
|
50 |
|
|
* match any object. The symbol doesn't really exist because it's an internal
|
51 |
|
|
* (i.e. not exported), and internally we never check its value. Since it's a
|
52 |
|
|
* symbol property, it's not expected to be unumerable, and the value is
|
53 |
|
|
* typed as always undefined, so its never expected to have a meaningful
|
54 |
|
|
* value anyway. It just makes this type distinquishable from plain `{}`.
|
55 |
|
|
*/
|
56 |
|
|
export type CombinedState<S> = { readonly [$CombinedState]?: undefined } & S
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* Recursively makes combined state objects partial. Only combined state _root
|
60 |
|
|
* objects_ (i.e. the generated higher level object with keys mapping to
|
61 |
|
|
* individual reducers) are partial.
|
62 |
|
|
*/
|
63 |
|
|
export type PreloadedState<S> = Required<S> extends {
|
64 |
|
|
[$CombinedState]: undefined
|
65 |
|
|
}
|
66 |
|
|
? S extends CombinedState<infer S1>
|
67 |
|
|
? {
|
68 |
|
|
[K in keyof S1]?: S1[K] extends object ? PreloadedState<S1[K]> : S1[K]
|
69 |
|
|
}
|
70 |
|
|
: never
|
71 |
|
|
: {
|
72 |
|
|
[K in keyof S]: S[K] extends object ? PreloadedState<S[K]> : S[K]
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/* reducers */
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* A *reducer* (also called a *reducing function*) is a function that accepts
|
79 |
|
|
* an accumulation and a value and returns a new accumulation. They are used
|
80 |
|
|
* to reduce a collection of values down to a single value
|
81 |
|
|
*
|
82 |
|
|
* Reducers are not unique to Redux—they are a fundamental concept in
|
83 |
|
|
* functional programming. Even most non-functional languages, like
|
84 |
|
|
* JavaScript, have a built-in API for reducing. In JavaScript, it's
|
85 |
|
|
* `Array.prototype.reduce()`.
|
86 |
|
|
*
|
87 |
|
|
* In Redux, the accumulated value is the state object, and the values being
|
88 |
|
|
* accumulated are actions. Reducers calculate a new state given the previous
|
89 |
|
|
* state and an action. They must be *pure functions*—functions that return
|
90 |
|
|
* the exact same output for given inputs. They should also be free of
|
91 |
|
|
* side-effects. This is what enables exciting features like hot reloading and
|
92 |
|
|
* time travel.
|
93 |
|
|
*
|
94 |
|
|
* Reducers are the most important concept in Redux.
|
95 |
|
|
*
|
96 |
|
|
* *Do not put API calls into reducers.*
|
97 |
|
|
*
|
98 |
|
|
* @template S The type of state consumed and produced by this reducer.
|
99 |
|
|
* @template A The type of actions the reducer can potentially respond to.
|
100 |
|
|
*/
|
101 |
|
|
export type Reducer<S = any, A extends Action = AnyAction> = (
|
102 |
|
|
state: S | undefined,
|
103 |
|
|
action: A
|
104 |
|
|
) => S
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Object whose values correspond to different reducer functions.
|
108 |
|
|
*
|
109 |
|
|
* @template A The type of actions the reducers can potentially respond to.
|
110 |
|
|
*/
|
111 |
|
|
export type ReducersMapObject<S = any, A extends Action = Action> = {
|
112 |
|
|
[K in keyof S]: Reducer<S[K], A>
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Infer a combined state shape from a `ReducersMapObject`.
|
117 |
|
|
*
|
118 |
|
|
* @template M Object map of reducers as provided to `combineReducers(map: M)`.
|
119 |
|
|
*/
|
120 |
|
|
export type StateFromReducersMapObject<M> = M extends ReducersMapObject<
|
121 |
|
|
any,
|
122 |
|
|
any
|
123 |
|
|
>
|
124 |
|
|
? { [P in keyof M]: M[P] extends Reducer<infer S, any> ? S : never }
|
125 |
|
|
: never
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Infer reducer union type from a `ReducersMapObject`.
|
129 |
|
|
*
|
130 |
|
|
* @template M Object map of reducers as provided to `combineReducers(map: M)`.
|
131 |
|
|
*/
|
132 |
|
|
export type ReducerFromReducersMapObject<M> = M extends {
|
133 |
|
|
[P in keyof M]: infer R
|
134 |
|
|
}
|
135 |
|
|
? R extends Reducer<any, any>
|
136 |
|
|
? R
|
137 |
|
|
: never
|
138 |
|
|
: never
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
|
|
* Infer action type from a reducer function.
|
142 |
|
|
*
|
143 |
|
|
* @template R Type of reducer.
|
144 |
|
|
*/
|
145 |
|
|
export type ActionFromReducer<R> = R extends Reducer<any, infer A> ? A : never
|
146 |
|
|
|
147 |
|
|
/**
|
148 |
|
|
* Infer action union type from a `ReducersMapObject`.
|
149 |
|
|
*
|
150 |
|
|
* @template M Object map of reducers as provided to `combineReducers(map: M)`.
|
151 |
|
|
*/
|
152 |
|
|
export type ActionFromReducersMapObject<M> = M extends ReducersMapObject<
|
153 |
|
|
any,
|
154 |
|
|
any
|
155 |
|
|
>
|
156 |
|
|
? ActionFromReducer<ReducerFromReducersMapObject<M>>
|
157 |
|
|
: never
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Turns an object whose values are different reducer functions, into a single
|
161 |
|
|
* reducer function. It will call every child reducer, and gather their results
|
162 |
|
|
* into a single state object, whose keys correspond to the keys of the passed
|
163 |
|
|
* reducer functions.
|
164 |
|
|
*
|
165 |
|
|
* @template S Combined state object type.
|
166 |
|
|
*
|
167 |
|
|
* @param reducers An object whose values correspond to different reducer
|
168 |
|
|
* functions that need to be combined into one. One handy way to obtain it
|
169 |
|
|
* is to use ES6 `import * as reducers` syntax. The reducers may never
|
170 |
|
|
* return undefined for any action. Instead, they should return their
|
171 |
|
|
* initial state if the state passed to them was undefined, and the current
|
172 |
|
|
* state for any unrecognized action.
|
173 |
|
|
*
|
174 |
|
|
* @returns A reducer function that invokes every reducer inside the passed
|
175 |
|
|
* object, and builds a state object with the same shape.
|
176 |
|
|
*/
|
177 |
|
|
export function combineReducers<S>(
|
178 |
|
|
reducers: ReducersMapObject<S, any>
|
179 |
|
|
): Reducer<CombinedState<S>>
|
180 |
|
|
export function combineReducers<S, A extends Action = AnyAction>(
|
181 |
|
|
reducers: ReducersMapObject<S, A>
|
182 |
|
|
): Reducer<CombinedState<S>, A>
|
183 |
|
|
export function combineReducers<M extends ReducersMapObject<any, any>>(
|
184 |
|
|
reducers: M
|
185 |
|
|
): Reducer<
|
186 |
|
|
CombinedState<StateFromReducersMapObject<M>>,
|
187 |
|
|
ActionFromReducersMapObject<M>
|
188 |
|
|
>
|
189 |
|
|
|
190 |
|
|
/* store */
|
191 |
|
|
|
192 |
|
|
/**
|
193 |
|
|
* A *dispatching function* (or simply *dispatch function*) is a function that
|
194 |
|
|
* accepts an action or an async action; it then may or may not dispatch one
|
195 |
|
|
* or more actions to the store.
|
196 |
|
|
*
|
197 |
|
|
* We must distinguish between dispatching functions in general and the base
|
198 |
|
|
* `dispatch` function provided by the store instance without any middleware.
|
199 |
|
|
*
|
200 |
|
|
* The base dispatch function *always* synchronously sends an action to the
|
201 |
|
|
* store's reducer, along with the previous state returned by the store, to
|
202 |
|
|
* calculate a new state. It expects actions to be plain objects ready to be
|
203 |
|
|
* consumed by the reducer.
|
204 |
|
|
*
|
205 |
|
|
* Middleware wraps the base dispatch function. It allows the dispatch
|
206 |
|
|
* function to handle async actions in addition to actions. Middleware may
|
207 |
|
|
* transform, delay, ignore, or otherwise interpret actions or async actions
|
208 |
|
|
* before passing them to the next middleware.
|
209 |
|
|
*
|
210 |
|
|
* @template A The type of things (actions or otherwise) which may be
|
211 |
|
|
* dispatched.
|
212 |
|
|
*/
|
213 |
|
|
export interface Dispatch<A extends Action = AnyAction> {
|
214 |
|
|
<T extends A>(action: T): T
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/**
|
218 |
|
|
* Function to remove listener added by `Store.subscribe()`.
|
219 |
|
|
*/
|
220 |
|
|
export interface Unsubscribe {
|
221 |
|
|
(): void
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
/**
|
225 |
|
|
* A minimal observable of state changes.
|
226 |
|
|
* For more information, see the observable proposal:
|
227 |
|
|
* https://github.com/tc39/proposal-observable
|
228 |
|
|
*/
|
229 |
|
|
export type Observable<T> = {
|
230 |
|
|
/**
|
231 |
|
|
* The minimal observable subscription method.
|
232 |
|
|
* @param {Object} observer Any object that can be used as an observer.
|
233 |
|
|
* The observer object should have a `next` method.
|
234 |
|
|
* @returns {subscription} An object with an `unsubscribe` method that can
|
235 |
|
|
* be used to unsubscribe the observable from the store, and prevent further
|
236 |
|
|
* emission of values from the observable.
|
237 |
|
|
*/
|
238 |
|
|
subscribe: (observer: Observer<T>) => { unsubscribe: Unsubscribe }
|
239 |
|
|
[Symbol.observable](): Observable<T>
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
/**
|
243 |
|
|
* An Observer is used to receive data from an Observable, and is supplied as
|
244 |
|
|
* an argument to subscribe.
|
245 |
|
|
*/
|
246 |
|
|
export type Observer<T> = {
|
247 |
|
|
next?(value: T): void
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* A store is an object that holds the application's state tree.
|
252 |
|
|
* There should only be a single store in a Redux app, as the composition
|
253 |
|
|
* happens on the reducer level.
|
254 |
|
|
*
|
255 |
|
|
* @template S The type of state held by this store.
|
256 |
|
|
* @template A the type of actions which may be dispatched by this store.
|
257 |
|
|
*/
|
258 |
|
|
export interface Store<S = any, A extends Action = AnyAction> {
|
259 |
|
|
/**
|
260 |
|
|
* Dispatches an action. It is the only way to trigger a state change.
|
261 |
|
|
*
|
262 |
|
|
* The `reducer` function, used to create the store, will be called with the
|
263 |
|
|
* current state tree and the given `action`. Its return value will be
|
264 |
|
|
* considered the **next** state of the tree, and the change listeners will
|
265 |
|
|
* be notified.
|
266 |
|
|
*
|
267 |
|
|
* The base implementation only supports plain object actions. If you want
|
268 |
|
|
* to dispatch a Promise, an Observable, a thunk, or something else, you
|
269 |
|
|
* need to wrap your store creating function into the corresponding
|
270 |
|
|
* middleware. For example, see the documentation for the `redux-thunk`
|
271 |
|
|
* package. Even the middleware will eventually dispatch plain object
|
272 |
|
|
* actions using this method.
|
273 |
|
|
*
|
274 |
|
|
* @param action A plain object representing “what changed”. It is a good
|
275 |
|
|
* idea to keep actions serializable so you can record and replay user
|
276 |
|
|
* sessions, or use the time travelling `redux-devtools`. An action must
|
277 |
|
|
* have a `type` property which may not be `undefined`. It is a good idea
|
278 |
|
|
* to use string constants for action types.
|
279 |
|
|
*
|
280 |
|
|
* @returns For convenience, the same action object you dispatched.
|
281 |
|
|
*
|
282 |
|
|
* Note that, if you use a custom middleware, it may wrap `dispatch()` to
|
283 |
|
|
* return something else (for example, a Promise you can await).
|
284 |
|
|
*/
|
285 |
|
|
dispatch: Dispatch<A>
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* Reads the state tree managed by the store.
|
289 |
|
|
*
|
290 |
|
|
* @returns The current state tree of your application.
|
291 |
|
|
*/
|
292 |
|
|
getState(): S
|
293 |
|
|
|
294 |
|
|
/**
|
295 |
|
|
* Adds a change listener. It will be called any time an action is
|
296 |
|
|
* dispatched, and some part of the state tree may potentially have changed.
|
297 |
|
|
* You may then call `getState()` to read the current state tree inside the
|
298 |
|
|
* callback.
|
299 |
|
|
*
|
300 |
|
|
* You may call `dispatch()` from a change listener, with the following
|
301 |
|
|
* caveats:
|
302 |
|
|
*
|
303 |
|
|
* 1. The subscriptions are snapshotted just before every `dispatch()` call.
|
304 |
|
|
* If you subscribe or unsubscribe while the listeners are being invoked,
|
305 |
|
|
* this will not have any effect on the `dispatch()` that is currently in
|
306 |
|
|
* progress. However, the next `dispatch()` call, whether nested or not,
|
307 |
|
|
* will use a more recent snapshot of the subscription list.
|
308 |
|
|
*
|
309 |
|
|
* 2. The listener should not expect to see all states changes, as the state
|
310 |
|
|
* might have been updated multiple times during a nested `dispatch()` before
|
311 |
|
|
* the listener is called. It is, however, guaranteed that all subscribers
|
312 |
|
|
* registered before the `dispatch()` started will be called with the latest
|
313 |
|
|
* state by the time it exits.
|
314 |
|
|
*
|
315 |
|
|
* @param listener A callback to be invoked on every dispatch.
|
316 |
|
|
* @returns A function to remove this change listener.
|
317 |
|
|
*/
|
318 |
|
|
subscribe(listener: () => void): Unsubscribe
|
319 |
|
|
|
320 |
|
|
/**
|
321 |
|
|
* Replaces the reducer currently used by the store to calculate the state.
|
322 |
|
|
*
|
323 |
|
|
* You might need this if your app implements code splitting and you want to
|
324 |
|
|
* load some of the reducers dynamically. You might also need this if you
|
325 |
|
|
* implement a hot reloading mechanism for Redux.
|
326 |
|
|
*
|
327 |
|
|
* @param nextReducer The reducer for the store to use instead.
|
328 |
|
|
*/
|
329 |
|
|
replaceReducer(nextReducer: Reducer<S, A>): void
|
330 |
|
|
|
331 |
|
|
/**
|
332 |
|
|
* Interoperability point for observable/reactive libraries.
|
333 |
|
|
* @returns {observable} A minimal observable of state changes.
|
334 |
|
|
* For more information, see the observable proposal:
|
335 |
|
|
* https://github.com/tc39/proposal-observable
|
336 |
|
|
*/
|
337 |
|
|
[Symbol.observable](): Observable<S>
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
export type DeepPartial<T> = {
|
341 |
|
|
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
/**
|
345 |
|
|
* A store creator is a function that creates a Redux store. Like with
|
346 |
|
|
* dispatching function, we must distinguish the base store creator,
|
347 |
|
|
* `createStore(reducer, preloadedState)` exported from the Redux package, from
|
348 |
|
|
* store creators that are returned from the store enhancers.
|
349 |
|
|
*
|
350 |
|
|
* @template S The type of state to be held by the store.
|
351 |
|
|
* @template A The type of actions which may be dispatched.
|
352 |
|
|
* @template Ext Store extension that is mixed in to the Store type.
|
353 |
|
|
* @template StateExt State extension that is mixed into the state type.
|
354 |
|
|
*/
|
355 |
|
|
export interface StoreCreator {
|
356 |
|
|
<S, A extends Action, Ext, StateExt>(
|
357 |
|
|
reducer: Reducer<S, A>,
|
358 |
|
|
enhancer?: StoreEnhancer<Ext, StateExt>
|
359 |
|
|
): Store<S & StateExt, A> & Ext
|
360 |
|
|
<S, A extends Action, Ext, StateExt>(
|
361 |
|
|
reducer: Reducer<S, A>,
|
362 |
|
|
preloadedState?: PreloadedState<S>,
|
363 |
|
|
enhancer?: StoreEnhancer<Ext>
|
364 |
|
|
): Store<S & StateExt, A> & Ext
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
/**
|
368 |
|
|
* Creates a Redux store that holds the state tree.
|
369 |
|
|
* The only way to change the data in the store is to call `dispatch()` on it.
|
370 |
|
|
*
|
371 |
|
|
* There should only be a single store in your app. To specify how different
|
372 |
|
|
* parts of the state tree respond to actions, you may combine several
|
373 |
|
|
* reducers
|
374 |
|
|
* into a single reducer function by using `combineReducers`.
|
375 |
|
|
*
|
376 |
|
|
* @template S State object type.
|
377 |
|
|
*
|
378 |
|
|
* @param reducer A function that returns the next state tree, given the
|
379 |
|
|
* current state tree and the action to handle.
|
380 |
|
|
*
|
381 |
|
|
* @param [preloadedState] The initial state. You may optionally specify it to
|
382 |
|
|
* hydrate the state from the server in universal apps, or to restore a
|
383 |
|
|
* previously serialized user session. If you use `combineReducers` to
|
384 |
|
|
* produce the root reducer function, this must be an object with the same
|
385 |
|
|
* shape as `combineReducers` keys.
|
386 |
|
|
*
|
387 |
|
|
* @param [enhancer] The store enhancer. You may optionally specify it to
|
388 |
|
|
* enhance the store with third-party capabilities such as middleware, time
|
389 |
|
|
* travel, persistence, etc. The only store enhancer that ships with Redux
|
390 |
|
|
* is `applyMiddleware()`.
|
391 |
|
|
*
|
392 |
|
|
* @returns A Redux store that lets you read the state, dispatch actions and
|
393 |
|
|
* subscribe to changes.
|
394 |
|
|
*/
|
395 |
|
|
export const createStore: StoreCreator
|
396 |
|
|
|
397 |
|
|
/**
|
398 |
|
|
* A store enhancer is a higher-order function that composes a store creator
|
399 |
|
|
* to return a new, enhanced store creator. This is similar to middleware in
|
400 |
|
|
* that it allows you to alter the store interface in a composable way.
|
401 |
|
|
*
|
402 |
|
|
* Store enhancers are much the same concept as higher-order components in
|
403 |
|
|
* React, which are also occasionally called “component enhancers”.
|
404 |
|
|
*
|
405 |
|
|
* Because a store is not an instance, but rather a plain-object collection of
|
406 |
|
|
* functions, copies can be easily created and modified without mutating the
|
407 |
|
|
* original store. There is an example in `compose` documentation
|
408 |
|
|
* demonstrating that.
|
409 |
|
|
*
|
410 |
|
|
* Most likely you'll never write a store enhancer, but you may use the one
|
411 |
|
|
* provided by the developer tools. It is what makes time travel possible
|
412 |
|
|
* without the app being aware it is happening. Amusingly, the Redux
|
413 |
|
|
* middleware implementation is itself a store enhancer.
|
414 |
|
|
*
|
415 |
|
|
* @template Ext Store extension that is mixed into the Store type.
|
416 |
|
|
* @template StateExt State extension that is mixed into the state type.
|
417 |
|
|
*/
|
418 |
|
|
export type StoreEnhancer<Ext = {}, StateExt = {}> = (
|
419 |
|
|
next: StoreEnhancerStoreCreator
|
420 |
|
|
) => StoreEnhancerStoreCreator<Ext, StateExt>
|
421 |
|
|
export type StoreEnhancerStoreCreator<Ext = {}, StateExt = {}> = <
|
422 |
|
|
S = any,
|
423 |
|
|
A extends Action = AnyAction
|
424 |
|
|
>(
|
425 |
|
|
reducer: Reducer<S, A>,
|
426 |
|
|
preloadedState?: PreloadedState<S>
|
427 |
|
|
) => Store<S & StateExt, A> & Ext
|
428 |
|
|
|
429 |
|
|
/* middleware */
|
430 |
|
|
|
431 |
|
|
export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
|
432 |
|
|
dispatch: D
|
433 |
|
|
getState(): S
|
434 |
|
|
}
|
435 |
|
|
|
436 |
|
|
/**
|
437 |
|
|
* A middleware is a higher-order function that composes a dispatch function
|
438 |
|
|
* to return a new dispatch function. It often turns async actions into
|
439 |
|
|
* actions.
|
440 |
|
|
*
|
441 |
|
|
* Middleware is composable using function composition. It is useful for
|
442 |
|
|
* logging actions, performing side effects like routing, or turning an
|
443 |
|
|
* asynchronous API call into a series of synchronous actions.
|
444 |
|
|
*
|
445 |
|
|
* @template DispatchExt Extra Dispatch signature added by this middleware.
|
446 |
|
|
* @template S The type of the state supported by this middleware.
|
447 |
|
|
* @template D The type of Dispatch of the store where this middleware is
|
448 |
|
|
* installed.
|
449 |
|
|
*/
|
450 |
|
|
export interface Middleware<
|
451 |
|
|
DispatchExt = {},
|
452 |
|
|
S = any,
|
453 |
|
|
D extends Dispatch = Dispatch
|
454 |
|
|
> {
|
455 |
|
|
(api: MiddlewareAPI<D, S>): (
|
456 |
|
|
next: Dispatch<AnyAction>
|
457 |
|
|
) => (action: any) => any
|
458 |
|
|
}
|
459 |
|
|
|
460 |
|
|
/**
|
461 |
|
|
* Creates a store enhancer that applies middleware to the dispatch method
|
462 |
|
|
* of the Redux store. This is handy for a variety of tasks, such as
|
463 |
|
|
* expressing asynchronous actions in a concise manner, or logging every
|
464 |
|
|
* action payload.
|
465 |
|
|
*
|
466 |
|
|
* See `redux-thunk` package as an example of the Redux middleware.
|
467 |
|
|
*
|
468 |
|
|
* Because middleware is potentially asynchronous, this should be the first
|
469 |
|
|
* store enhancer in the composition chain.
|
470 |
|
|
*
|
471 |
|
|
* Note that each middleware will be given the `dispatch` and `getState`
|
472 |
|
|
* functions as named arguments.
|
473 |
|
|
*
|
474 |
|
|
* @param middlewares The middleware chain to be applied.
|
475 |
|
|
* @returns A store enhancer applying the middleware.
|
476 |
|
|
*
|
477 |
|
|
* @template Ext Dispatch signature added by a middleware.
|
478 |
|
|
* @template S The type of the state supported by a middleware.
|
479 |
|
|
*/
|
480 |
|
|
export function applyMiddleware(): StoreEnhancer
|
481 |
|
|
export function applyMiddleware<Ext1, S>(
|
482 |
|
|
middleware1: Middleware<Ext1, S, any>
|
483 |
|
|
): StoreEnhancer<{ dispatch: Ext1 }>
|
484 |
|
|
export function applyMiddleware<Ext1, Ext2, S>(
|
485 |
|
|
middleware1: Middleware<Ext1, S, any>,
|
486 |
|
|
middleware2: Middleware<Ext2, S, any>
|
487 |
|
|
): StoreEnhancer<{ dispatch: Ext1 & Ext2 }>
|
488 |
|
|
export function applyMiddleware<Ext1, Ext2, Ext3, S>(
|
489 |
|
|
middleware1: Middleware<Ext1, S, any>,
|
490 |
|
|
middleware2: Middleware<Ext2, S, any>,
|
491 |
|
|
middleware3: Middleware<Ext3, S, any>
|
492 |
|
|
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 }>
|
493 |
|
|
export function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>(
|
494 |
|
|
middleware1: Middleware<Ext1, S, any>,
|
495 |
|
|
middleware2: Middleware<Ext2, S, any>,
|
496 |
|
|
middleware3: Middleware<Ext3, S, any>,
|
497 |
|
|
middleware4: Middleware<Ext4, S, any>
|
498 |
|
|
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }>
|
499 |
|
|
export function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(
|
500 |
|
|
middleware1: Middleware<Ext1, S, any>,
|
501 |
|
|
middleware2: Middleware<Ext2, S, any>,
|
502 |
|
|
middleware3: Middleware<Ext3, S, any>,
|
503 |
|
|
middleware4: Middleware<Ext4, S, any>,
|
504 |
|
|
middleware5: Middleware<Ext5, S, any>
|
505 |
|
|
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }>
|
506 |
|
|
export function applyMiddleware<Ext, S = any>(
|
507 |
|
|
...middlewares: Middleware<any, S, any>[]
|
508 |
|
|
): StoreEnhancer<{ dispatch: Ext }>
|
509 |
|
|
|
510 |
|
|
/* action creators */
|
511 |
|
|
|
512 |
|
|
/**
|
513 |
|
|
* An *action creator* is, quite simply, a function that creates an action. Do
|
514 |
|
|
* not confuse the two terms—again, an action is a payload of information, and
|
515 |
|
|
* an action creator is a factory that creates an action.
|
516 |
|
|
*
|
517 |
|
|
* Calling an action creator only produces an action, but does not dispatch
|
518 |
|
|
* it. You need to call the store's `dispatch` function to actually cause the
|
519 |
|
|
* mutation. Sometimes we say *bound action creators* to mean functions that
|
520 |
|
|
* call an action creator and immediately dispatch its result to a specific
|
521 |
|
|
* store instance.
|
522 |
|
|
*
|
523 |
|
|
* If an action creator needs to read the current state, perform an API call,
|
524 |
|
|
* or cause a side effect, like a routing transition, it should return an
|
525 |
|
|
* async action instead of an action.
|
526 |
|
|
*
|
527 |
|
|
* @template A Returned action type.
|
528 |
|
|
*/
|
529 |
|
|
export interface ActionCreator<A> {
|
530 |
|
|
(...args: any[]): A
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
/**
|
534 |
|
|
* Object whose values are action creator functions.
|
535 |
|
|
*/
|
536 |
|
|
export interface ActionCreatorsMapObject<A = any> {
|
537 |
|
|
[key: string]: ActionCreator<A>
|
538 |
|
|
}
|
539 |
|
|
|
540 |
|
|
/**
|
541 |
|
|
* Turns an object whose values are action creators, into an object with the
|
542 |
|
|
* same keys, but with every function wrapped into a `dispatch` call so they
|
543 |
|
|
* may be invoked directly. This is just a convenience method, as you can call
|
544 |
|
|
* `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
|
545 |
|
|
*
|
546 |
|
|
* For convenience, you can also pass a single function as the first argument,
|
547 |
|
|
* and get a function in return.
|
548 |
|
|
*
|
549 |
|
|
* @param actionCreator An object whose values are action creator functions.
|
550 |
|
|
* One handy way to obtain it is to use ES6 `import * as` syntax. You may
|
551 |
|
|
* also pass a single function.
|
552 |
|
|
*
|
553 |
|
|
* @param dispatch The `dispatch` function available on your Redux store.
|
554 |
|
|
*
|
555 |
|
|
* @returns The object mimicking the original object, but with every action
|
556 |
|
|
* creator wrapped into the `dispatch` call. If you passed a function as
|
557 |
|
|
* `actionCreator`, the return value will also be a single function.
|
558 |
|
|
*/
|
559 |
|
|
export function bindActionCreators<A, C extends ActionCreator<A>>(
|
560 |
|
|
actionCreator: C,
|
561 |
|
|
dispatch: Dispatch
|
562 |
|
|
): C
|
563 |
|
|
|
564 |
|
|
export function bindActionCreators<
|
565 |
|
|
A extends ActionCreator<any>,
|
566 |
|
|
B extends ActionCreator<any>
|
567 |
|
|
>(actionCreator: A, dispatch: Dispatch): B
|
568 |
|
|
|
569 |
|
|
export function bindActionCreators<A, M extends ActionCreatorsMapObject<A>>(
|
570 |
|
|
actionCreators: M,
|
571 |
|
|
dispatch: Dispatch
|
572 |
|
|
): M
|
573 |
|
|
|
574 |
|
|
export function bindActionCreators<
|
575 |
|
|
M extends ActionCreatorsMapObject<any>,
|
576 |
|
|
N extends ActionCreatorsMapObject<any>
|
577 |
|
|
>(actionCreators: M, dispatch: Dispatch): N
|
578 |
|
|
|
579 |
|
|
/* compose */
|
580 |
|
|
|
581 |
|
|
type Func0<R> = () => R
|
582 |
|
|
type Func1<T1, R> = (a1: T1) => R
|
583 |
|
|
type Func2<T1, T2, R> = (a1: T1, a2: T2) => R
|
584 |
|
|
type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R
|
585 |
|
|
|
586 |
|
|
/**
|
587 |
|
|
* Composes single-argument functions from right to left. The rightmost
|
588 |
|
|
* function can take multiple arguments as it provides the signature for the
|
589 |
|
|
* resulting composite function.
|
590 |
|
|
*
|
591 |
|
|
* @param funcs The functions to compose.
|
592 |
|
|
* @returns R function obtained by composing the argument functions from right
|
593 |
|
|
* to left. For example, `compose(f, g, h)` is identical to doing
|
594 |
|
|
* `(...args) => f(g(h(...args)))`.
|
595 |
|
|
*/
|
596 |
|
|
export function compose(): <R>(a: R) => R
|
597 |
|
|
|
598 |
|
|
export function compose<F extends Function>(f: F): F
|
599 |
|
|
|
600 |
|
|
/* two functions */
|
601 |
|
|
export function compose<A, R>(f1: (b: A) => R, f2: Func0<A>): Func0<R>
|
602 |
|
|
export function compose<A, T1, R>(
|
603 |
|
|
f1: (b: A) => R,
|
604 |
|
|
f2: Func1<T1, A>
|
605 |
|
|
): Func1<T1, R>
|
606 |
|
|
export function compose<A, T1, T2, R>(
|
607 |
|
|
f1: (b: A) => R,
|
608 |
|
|
f2: Func2<T1, T2, A>
|
609 |
|
|
): Func2<T1, T2, R>
|
610 |
|
|
export function compose<A, T1, T2, T3, R>(
|
611 |
|
|
f1: (b: A) => R,
|
612 |
|
|
f2: Func3<T1, T2, T3, A>
|
613 |
|
|
): Func3<T1, T2, T3, R>
|
614 |
|
|
|
615 |
|
|
/* three functions */
|
616 |
|
|
export function compose<A, B, R>(
|
617 |
|
|
f1: (b: B) => R,
|
618 |
|
|
f2: (a: A) => B,
|
619 |
|
|
f3: Func0<A>
|
620 |
|
|
): Func0<R>
|
621 |
|
|
export function compose<A, B, T1, R>(
|
622 |
|
|
f1: (b: B) => R,
|
623 |
|
|
f2: (a: A) => B,
|
624 |
|
|
f3: Func1<T1, A>
|
625 |
|
|
): Func1<T1, R>
|
626 |
|
|
export function compose<A, B, T1, T2, R>(
|
627 |
|
|
f1: (b: B) => R,
|
628 |
|
|
f2: (a: A) => B,
|
629 |
|
|
f3: Func2<T1, T2, A>
|
630 |
|
|
): Func2<T1, T2, R>
|
631 |
|
|
export function compose<A, B, T1, T2, T3, R>(
|
632 |
|
|
f1: (b: B) => R,
|
633 |
|
|
f2: (a: A) => B,
|
634 |
|
|
f3: Func3<T1, T2, T3, A>
|
635 |
|
|
): Func3<T1, T2, T3, R>
|
636 |
|
|
|
637 |
|
|
/* four functions */
|
638 |
|
|
export function compose<A, B, C, R>(
|
639 |
|
|
f1: (b: C) => R,
|
640 |
|
|
f2: (a: B) => C,
|
641 |
|
|
f3: (a: A) => B,
|
642 |
|
|
f4: Func0<A>
|
643 |
|
|
): Func0<R>
|
644 |
|
|
export function compose<A, B, C, T1, R>(
|
645 |
|
|
f1: (b: C) => R,
|
646 |
|
|
f2: (a: B) => C,
|
647 |
|
|
f3: (a: A) => B,
|
648 |
|
|
f4: Func1<T1, A>
|
649 |
|
|
): Func1<T1, R>
|
650 |
|
|
export function compose<A, B, C, T1, T2, R>(
|
651 |
|
|
f1: (b: C) => R,
|
652 |
|
|
f2: (a: B) => C,
|
653 |
|
|
f3: (a: A) => B,
|
654 |
|
|
f4: Func2<T1, T2, A>
|
655 |
|
|
): Func2<T1, T2, R>
|
656 |
|
|
export function compose<A, B, C, T1, T2, T3, R>(
|
657 |
|
|
f1: (b: C) => R,
|
658 |
|
|
f2: (a: B) => C,
|
659 |
|
|
f3: (a: A) => B,
|
660 |
|
|
f4: Func3<T1, T2, T3, A>
|
661 |
|
|
): Func3<T1, T2, T3, R>
|
662 |
|
|
|
663 |
|
|
/* rest */
|
664 |
|
|
export function compose<R>(
|
665 |
|
|
f1: (b: any) => R,
|
666 |
|
|
...funcs: Function[]
|
667 |
|
|
): (...args: any[]) => R
|
668 |
|
|
|
669 |
|
|
export function compose<R>(...funcs: Function[]): (...args: any[]) => R
|