UNPKG

1.8 kBJavaScriptView Raw
1var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
2
3import compose from './compose';
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 {...Function} middlewares The middleware chain to be applied.
19 * @returns {Function} A store enhancer applying the middleware.
20 */
21export default function applyMiddleware() {
22 for (var _len = arguments.length, middlewares = Array(_len), _key = 0; _key < _len; _key++) {
23 middlewares[_key] = arguments[_key];
24 }
25
26 return function (createStore) {
27 return function (reducer, preloadedState, enhancer) {
28 var store = createStore(reducer, preloadedState, enhancer);
29 var _dispatch = store.dispatch;
30 var chain = [];
31
32 var middlewareAPI = {
33 getState: store.getState,
34 dispatch: function dispatch(action) {
35 return _dispatch(action);
36 }
37 };
38 chain = middlewares.map(function (middleware) {
39 return middleware(middlewareAPI);
40 });
41 _dispatch = compose.apply(undefined, chain)(store.dispatch);
42
43 return _extends({}, store, {
44 dispatch: _dispatch
45 });
46 };
47 };
48}
\No newline at end of file