1 | import compose from './compose'
|
2 | import { Middleware, MiddlewareAPI } from './types/middleware'
|
3 | import { StoreEnhancer, Dispatch } from './types/store'
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export default function applyMiddleware(): StoreEnhancer
|
25 | export default function applyMiddleware<Ext1, S>(
|
26 | middleware1: Middleware<Ext1, S, any>
|
27 | ): StoreEnhancer<{ dispatch: Ext1 }>
|
28 | export default function applyMiddleware<Ext1, Ext2, S>(
|
29 | middleware1: Middleware<Ext1, S, any>,
|
30 | middleware2: Middleware<Ext2, S, any>
|
31 | ): StoreEnhancer<{ dispatch: Ext1 & Ext2 }>
|
32 | export 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 }>
|
37 | export 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 }>
|
43 | export 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 }>
|
50 | export default function applyMiddleware<Ext, S = any>(
|
51 | ...middlewares: Middleware<any, S, any>[]
|
52 | ): StoreEnhancer<{ dispatch: Ext }>
|
53 | export 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 | }
|