UNPKG

2.04 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _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; };
6
7exports['default'] = applyMiddleware;
8
9var _compose = require('./compose');
10
11var _compose2 = _interopRequireDefault(_compose);
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
14
15/**
16 * Creates a store enhancer that applies middleware to the dispatch method
17 * of the Redux store. This is handy for a variety of tasks, such as expressing
18 * asynchronous actions in a concise manner, or logging every action payload.
19 *
20 * See `redux-thunk` package as an example of the Redux middleware.
21 *
22 * Because middleware is potentially asynchronous, this should be the first
23 * store enhancer in the composition chain.
24 *
25 * Note that each middleware will be given the `dispatch` and `getState` functions
26 * as named arguments.
27 *
28 * @param {...Function} middlewares The middleware chain to be applied.
29 * @returns {Function} A store enhancer applying the middleware.
30 */
31function applyMiddleware() {
32 for (var _len = arguments.length, middlewares = Array(_len), _key = 0; _key < _len; _key++) {
33 middlewares[_key] = arguments[_key];
34 }
35
36 return function (createStore) {
37 return function (reducer, preloadedState, enhancer) {
38 var store = createStore(reducer, preloadedState, enhancer);
39 var _dispatch = store.dispatch;
40 var chain = [];
41
42 var middlewareAPI = {
43 getState: store.getState,
44 dispatch: function dispatch(action) {
45 return _dispatch(action);
46 }
47 };
48 chain = middlewares.map(function (middleware) {
49 return middleware(middlewareAPI);
50 });
51 _dispatch = _compose2['default'].apply(undefined, chain)(store.dispatch);
52
53 return _extends({}, store, {
54 dispatch: _dispatch
55 });
56 };
57 };
58}
\No newline at end of file