( ...middlewares: Middleware[] )
| 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 | } |
no test coverage detected