1
1
import compose from './compose'
2
+ import {
3
+ Middleware ,
4
+ StoreEnhancer ,
5
+ StoreCreator ,
6
+ AnyAction ,
7
+ Reducer ,
8
+ Dispatch ,
9
+ MiddlewareAPI
10
+ } from '..'
2
11
3
12
/**
4
13
* Creates a store enhancer that applies middleware to the dispatch method
@@ -16,19 +25,24 @@ import compose from './compose'
16
25
* @param {...Function } middlewares The middleware chain to be applied.
17
26
* @returns {Function } A store enhancer applying the middleware.
18
27
*/
19
- export default function applyMiddleware ( ...middlewares ) {
20
- return createStore => ( ...args ) => {
21
- const store = createStore ( ...args )
22
- let dispatch = ( ) => {
28
+ export default function applyMiddleware (
29
+ ...middlewares : Middleware [ ]
30
+ ) : StoreEnhancer {
31
+ return ( createStore : StoreCreator ) => < S , A extends AnyAction > (
32
+ reducer : Reducer < S , A > ,
33
+ ...args : any [ ]
34
+ ) => {
35
+ const store = createStore ( reducer , ...args )
36
+ let dispatch : Dispatch = ( ) => {
23
37
throw new Error (
24
38
'Dispatching while constructing your middleware is not allowed. ' +
25
39
'Other middleware would not be applied to this dispatch.'
26
40
)
27
41
}
28
42
29
- const middlewareAPI = {
43
+ const middlewareAPI : MiddlewareAPI = {
30
44
getState : store . getState ,
31
- dispatch : ( ...args ) => dispatch ( ...args )
45
+ dispatch : ( action , ...args ) => dispatch ( action , ...args )
32
46
}
33
47
const chain = middlewares . map ( middleware => middleware ( middlewareAPI ) )
34
48
dispatch = compose ( ...chain ) ( store . dispatch )
0 commit comments