UNPKG

3.07 kBPlain TextView Raw
1import compose from './compose'
2import { Middleware, MiddlewareAPI } from './types/middleware'
3import { StoreEnhancer, Dispatch } from './types/store'
4
5/**
6 * Creates a store enhancer that applies middleware to the dispatch method
7 * of the Redux store. This is handy for a variety of tasks, such as expressing
8 * asynchronous actions in a concise manner, or logging every action payload.
9 *
10 * See `redux-thunk` package as an example of the Redux middleware.
11 *
12 * Because middleware is potentially asynchronous, this should be the first
13 * store enhancer in the composition chain.
14 *
15 * Note that each middleware will be given the `dispatch` and `getState` functions
16 * as named arguments.
17 *
18 * @param middlewares The middleware chain to be applied.
19 * @returns A store enhancer applying the middleware.
20 *
21 * @template Ext Dispatch signature added by a middleware.
22 * @template S The type of the state supported by a middleware.
23 */
24export default function applyMiddleware(): StoreEnhancer
25export default function applyMiddleware<Ext1, S>(
26 middleware1: Middleware<Ext1, S, any>
27): StoreEnhancer<{ dispatch: Ext1 }>
28export default function applyMiddleware<Ext1, Ext2, S>(
29 middleware1: Middleware<Ext1, S, any>,
30 middleware2: Middleware<Ext2, S, any>
31): StoreEnhancer<{ dispatch: Ext1 & Ext2 }>
32export default function applyMiddleware<Ext1, Ext2, Ext3, S>(
33 middleware1: Middleware<Ext1, S, any>,
34 middleware2: Middleware<Ext2, S, any>,
35 middleware3: Middleware<Ext3, S, any>
36): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 }>
37export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>(
38 middleware1: Middleware<Ext1, S, any>,
39 middleware2: Middleware<Ext2, S, any>,
40 middleware3: Middleware<Ext3, S, any>,
41 middleware4: Middleware<Ext4, S, any>
42): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }>
43export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(
44 middleware1: Middleware<Ext1, S, any>,
45 middleware2: Middleware<Ext2, S, any>,
46 middleware3: Middleware<Ext3, S, any>,
47 middleware4: Middleware<Ext4, S, any>,
48 middleware5: Middleware<Ext5, S, any>
49): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }>
50export default function applyMiddleware<Ext, S = any>(
51 ...middlewares: Middleware<any, S, any>[]
52): StoreEnhancer<{ dispatch: Ext }>
53export default function applyMiddleware(
54 ...middlewares: Middleware[]
55): StoreEnhancer<any> {
56 return createStore => (reducer, preloadedState) => {
57 const store = createStore(reducer, preloadedState)
58 let dispatch: Dispatch = () => {
59 throw new Error(
60 'Dispatching while constructing your middleware is not allowed. ' +
61 'Other middleware would not be applied to this dispatch.'
62 )
63 }
64
65 const middlewareAPI: MiddlewareAPI = {
66 getState: store.getState,
67 dispatch: (action, ...args) => dispatch(action, ...args)
68 }
69 const chain = middlewares.map(middleware => middleware(middlewareAPI))
70 dispatch = compose<typeof dispatch>(...chain)(store.dispatch)
71
72 return {
73 ...store,
74 dispatch
75 }
76 }
77}