redux.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = global || self, factory(global.Redux = {}));
  5. }(this, function (exports) { 'use strict';
  6. function symbolObservablePonyfill(root) {
  7. var result;
  8. var Symbol = root.Symbol;
  9. if (typeof Symbol === 'function') {
  10. if (Symbol.observable) {
  11. result = Symbol.observable;
  12. } else {
  13. result = Symbol('observable');
  14. Symbol.observable = result;
  15. }
  16. } else {
  17. result = '@@observable';
  18. }
  19. return result;
  20. }
  21. /* global window */
  22. var root;
  23. if (typeof self !== 'undefined') {
  24. root = self;
  25. } else if (typeof window !== 'undefined') {
  26. root = window;
  27. } else if (typeof global !== 'undefined') {
  28. root = global;
  29. } else if (typeof module !== 'undefined') {
  30. root = module;
  31. } else {
  32. root = Function('return this')();
  33. }
  34. var result = symbolObservablePonyfill(root);
  35. /**
  36. * These are private action types reserved by Redux.
  37. * For any unknown actions, you must return the current state.
  38. * If the current state is undefined, you must return the initial state.
  39. * Do not reference these action types directly in your code.
  40. */
  41. var randomString = function randomString() {
  42. return Math.random().toString(36).substring(7).split('').join('.');
  43. };
  44. var ActionTypes = {
  45. INIT: "@@redux/INIT" + randomString(),
  46. REPLACE: "@@redux/REPLACE" + randomString(),
  47. PROBE_UNKNOWN_ACTION: function PROBE_UNKNOWN_ACTION() {
  48. return "@@redux/PROBE_UNKNOWN_ACTION" + randomString();
  49. }
  50. };
  51. /**
  52. * @param {any} obj The object to inspect.
  53. * @returns {boolean} True if the argument appears to be a plain object.
  54. */
  55. function isPlainObject(obj) {
  56. if (typeof obj !== 'object' || obj === null) return false;
  57. var proto = obj;
  58. while (Object.getPrototypeOf(proto) !== null) {
  59. proto = Object.getPrototypeOf(proto);
  60. }
  61. return Object.getPrototypeOf(obj) === proto;
  62. }
  63. /**
  64. * Creates a Redux store that holds the state tree.
  65. * The only way to change the data in the store is to call `dispatch()` on it.
  66. *
  67. * There should only be a single store in your app. To specify how different
  68. * parts of the state tree respond to actions, you may combine several reducers
  69. * into a single reducer function by using `combineReducers`.
  70. *
  71. * @param {Function} reducer A function that returns the next state tree, given
  72. * the current state tree and the action to handle.
  73. *
  74. * @param {any} [preloadedState] The initial state. You may optionally specify it
  75. * to hydrate the state from the server in universal apps, or to restore a
  76. * previously serialized user session.
  77. * If you use `combineReducers` to produce the root reducer function, this must be
  78. * an object with the same shape as `combineReducers` keys.
  79. *
  80. * @param {Function} [enhancer] The store enhancer. You may optionally specify it
  81. * to enhance the store with third-party capabilities such as middleware,
  82. * time travel, persistence, etc. The only store enhancer that ships with Redux
  83. * is `applyMiddleware()`.
  84. *
  85. * @returns {Store} A Redux store that lets you read the state, dispatch actions
  86. * and subscribe to changes.
  87. */
  88. function createStore(reducer, preloadedState, enhancer) {
  89. var _ref2;
  90. if (typeof preloadedState === 'function' && typeof enhancer === 'function' || typeof enhancer === 'function' && typeof arguments[3] === 'function') {
  91. throw new Error('It looks like you are passing several store enhancers to ' + 'createStore(). This is not supported. Instead, compose them ' + 'together to a single function.');
  92. }
  93. if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
  94. enhancer = preloadedState;
  95. preloadedState = undefined;
  96. }
  97. if (typeof enhancer !== 'undefined') {
  98. if (typeof enhancer !== 'function') {
  99. throw new Error('Expected the enhancer to be a function.');
  100. }
  101. return enhancer(createStore)(reducer, preloadedState);
  102. }
  103. if (typeof reducer !== 'function') {
  104. throw new Error('Expected the reducer to be a function.');
  105. }
  106. var currentReducer = reducer;
  107. var currentState = preloadedState;
  108. var currentListeners = [];
  109. var nextListeners = currentListeners;
  110. var isDispatching = false;
  111. /**
  112. * This makes a shallow copy of currentListeners so we can use
  113. * nextListeners as a temporary list while dispatching.
  114. *
  115. * This prevents any bugs around consumers calling
  116. * subscribe/unsubscribe in the middle of a dispatch.
  117. */
  118. function ensureCanMutateNextListeners() {
  119. if (nextListeners === currentListeners) {
  120. nextListeners = currentListeners.slice();
  121. }
  122. }
  123. /**
  124. * Reads the state tree managed by the store.
  125. *
  126. * @returns {any} The current state tree of your application.
  127. */
  128. function getState() {
  129. if (isDispatching) {
  130. throw new Error('You may not call store.getState() while the reducer is executing. ' + 'The reducer has already received the state as an argument. ' + 'Pass it down from the top reducer instead of reading it from the store.');
  131. }
  132. return currentState;
  133. }
  134. /**
  135. * Adds a change listener. It will be called any time an action is dispatched,
  136. * and some part of the state tree may potentially have changed. You may then
  137. * call `getState()` to read the current state tree inside the callback.
  138. *
  139. * You may call `dispatch()` from a change listener, with the following
  140. * caveats:
  141. *
  142. * 1. The subscriptions are snapshotted just before every `dispatch()` call.
  143. * If you subscribe or unsubscribe while the listeners are being invoked, this
  144. * will not have any effect on the `dispatch()` that is currently in progress.
  145. * However, the next `dispatch()` call, whether nested or not, will use a more
  146. * recent snapshot of the subscription list.
  147. *
  148. * 2. The listener should not expect to see all state changes, as the state
  149. * might have been updated multiple times during a nested `dispatch()` before
  150. * the listener is called. It is, however, guaranteed that all subscribers
  151. * registered before the `dispatch()` started will be called with the latest
  152. * state by the time it exits.
  153. *
  154. * @param {Function} listener A callback to be invoked on every dispatch.
  155. * @returns {Function} A function to remove this change listener.
  156. */
  157. function subscribe(listener) {
  158. if (typeof listener !== 'function') {
  159. throw new Error('Expected the listener to be a function.');
  160. }
  161. if (isDispatching) {
  162. throw new Error('You may not call store.subscribe() while the reducer is executing. ' + 'If you would like to be notified after the store has been updated, subscribe from a ' + 'component and invoke store.getState() in the callback to access the latest state. ' + 'See https://redux.js.org/api-reference/store#subscribelistener for more details.');
  163. }
  164. var isSubscribed = true;
  165. ensureCanMutateNextListeners();
  166. nextListeners.push(listener);
  167. return function unsubscribe() {
  168. if (!isSubscribed) {
  169. return;
  170. }
  171. if (isDispatching) {
  172. throw new Error('You may not unsubscribe from a store listener while the reducer is executing. ' + 'See https://redux.js.org/api-reference/store#subscribelistener for more details.');
  173. }
  174. isSubscribed = false;
  175. ensureCanMutateNextListeners();
  176. var index = nextListeners.indexOf(listener);
  177. nextListeners.splice(index, 1);
  178. currentListeners = null;
  179. };
  180. }
  181. /**
  182. * Dispatches an action. It is the only way to trigger a state change.
  183. *
  184. * The `reducer` function, used to create the store, will be called with the
  185. * current state tree and the given `action`. Its return value will
  186. * be considered the **next** state of the tree, and the change listeners
  187. * will be notified.
  188. *
  189. * The base implementation only supports plain object actions. If you want to
  190. * dispatch a Promise, an Observable, a thunk, or something else, you need to
  191. * wrap your store creating function into the corresponding middleware. For
  192. * example, see the documentation for the `redux-thunk` package. Even the
  193. * middleware will eventually dispatch plain object actions using this method.
  194. *
  195. * @param {Object} action A plain object representing “what changed”. It is
  196. * a good idea to keep actions serializable so you can record and replay user
  197. * sessions, or use the time travelling `redux-devtools`. An action must have
  198. * a `type` property which may not be `undefined`. It is a good idea to use
  199. * string constants for action types.
  200. *
  201. * @returns {Object} For convenience, the same action object you dispatched.
  202. *
  203. * Note that, if you use a custom middleware, it may wrap `dispatch()` to
  204. * return something else (for example, a Promise you can await).
  205. */
  206. function dispatch(action) {
  207. if (!isPlainObject(action)) {
  208. throw new Error('Actions must be plain objects. ' + 'Use custom middleware for async actions.');
  209. }
  210. if (typeof action.type === 'undefined') {
  211. throw new Error('Actions may not have an undefined "type" property. ' + 'Have you misspelled a constant?');
  212. }
  213. if (isDispatching) {
  214. throw new Error('Reducers may not dispatch actions.');
  215. }
  216. try {
  217. isDispatching = true;
  218. currentState = currentReducer(currentState, action);
  219. } finally {
  220. isDispatching = false;
  221. }
  222. var listeners = currentListeners = nextListeners;
  223. for (var i = 0; i < listeners.length; i++) {
  224. var listener = listeners[i];
  225. listener();
  226. }
  227. return action;
  228. }
  229. /**
  230. * Replaces the reducer currently used by the store to calculate the state.
  231. *
  232. * You might need this if your app implements code splitting and you want to
  233. * load some of the reducers dynamically. You might also need this if you
  234. * implement a hot reloading mechanism for Redux.
  235. *
  236. * @param {Function} nextReducer The reducer for the store to use instead.
  237. * @returns {void}
  238. */
  239. function replaceReducer(nextReducer) {
  240. if (typeof nextReducer !== 'function') {
  241. throw new Error('Expected the nextReducer to be a function.');
  242. }
  243. currentReducer = nextReducer; // This action has a similiar effect to ActionTypes.INIT.
  244. // Any reducers that existed in both the new and old rootReducer
  245. // will receive the previous state. This effectively populates
  246. // the new state tree with any relevant data from the old one.
  247. dispatch({
  248. type: ActionTypes.REPLACE
  249. });
  250. }
  251. /**
  252. * Interoperability point for observable/reactive libraries.
  253. * @returns {observable} A minimal observable of state changes.
  254. * For more information, see the observable proposal:
  255. * https://github.com/tc39/proposal-observable
  256. */
  257. function observable() {
  258. var _ref;
  259. var outerSubscribe = subscribe;
  260. return _ref = {
  261. /**
  262. * The minimal observable subscription method.
  263. * @param {Object} observer Any object that can be used as an observer.
  264. * The observer object should have a `next` method.
  265. * @returns {subscription} An object with an `unsubscribe` method that can
  266. * be used to unsubscribe the observable from the store, and prevent further
  267. * emission of values from the observable.
  268. */
  269. subscribe: function subscribe(observer) {
  270. if (typeof observer !== 'object' || observer === null) {
  271. throw new TypeError('Expected the observer to be an object.');
  272. }
  273. function observeState() {
  274. if (observer.next) {
  275. observer.next(getState());
  276. }
  277. }
  278. observeState();
  279. var unsubscribe = outerSubscribe(observeState);
  280. return {
  281. unsubscribe: unsubscribe
  282. };
  283. }
  284. }, _ref[result] = function () {
  285. return this;
  286. }, _ref;
  287. } // When a store is created, an "INIT" action is dispatched so that every
  288. // reducer returns their initial state. This effectively populates
  289. // the initial state tree.
  290. dispatch({
  291. type: ActionTypes.INIT
  292. });
  293. return _ref2 = {
  294. dispatch: dispatch,
  295. subscribe: subscribe,
  296. getState: getState,
  297. replaceReducer: replaceReducer
  298. }, _ref2[result] = observable, _ref2;
  299. }
  300. /**
  301. * Prints a warning in the console if it exists.
  302. *
  303. * @param {String} message The warning message.
  304. * @returns {void}
  305. */
  306. function warning(message) {
  307. /* eslint-disable no-console */
  308. if (typeof console !== 'undefined' && typeof console.error === 'function') {
  309. console.error(message);
  310. }
  311. /* eslint-enable no-console */
  312. try {
  313. // This error was thrown as a convenience so that if you enable
  314. // "break on all exceptions" in your console,
  315. // it would pause the execution at this line.
  316. throw new Error(message);
  317. } catch (e) {} // eslint-disable-line no-empty
  318. }
  319. function getUndefinedStateErrorMessage(key, action) {
  320. var actionType = action && action.type;
  321. var actionDescription = actionType && "action \"" + String(actionType) + "\"" || 'an action';
  322. return "Given " + actionDescription + ", reducer \"" + key + "\" returned undefined. " + "To ignore an action, you must explicitly return the previous state. " + "If you want this reducer to hold no value, you can return null instead of undefined.";
  323. }
  324. function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
  325. var reducerKeys = Object.keys(reducers);
  326. var argumentName = action && action.type === ActionTypes.INIT ? 'preloadedState argument passed to createStore' : 'previous state received by the reducer';
  327. if (reducerKeys.length === 0) {
  328. return 'Store does not have a valid reducer. Make sure the argument passed ' + 'to combineReducers is an object whose values are reducers.';
  329. }
  330. if (!isPlainObject(inputState)) {
  331. return "The " + argumentName + " has unexpected type of \"" + {}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] + "\". Expected argument to be an object with the following " + ("keys: \"" + reducerKeys.join('", "') + "\"");
  332. }
  333. var unexpectedKeys = Object.keys(inputState).filter(function (key) {
  334. return !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key];
  335. });
  336. unexpectedKeys.forEach(function (key) {
  337. unexpectedKeyCache[key] = true;
  338. });
  339. if (action && action.type === ActionTypes.REPLACE) return;
  340. if (unexpectedKeys.length > 0) {
  341. return "Unexpected " + (unexpectedKeys.length > 1 ? 'keys' : 'key') + " " + ("\"" + unexpectedKeys.join('", "') + "\" found in " + argumentName + ". ") + "Expected to find one of the known reducer keys instead: " + ("\"" + reducerKeys.join('", "') + "\". Unexpected keys will be ignored.");
  342. }
  343. }
  344. function assertReducerShape(reducers) {
  345. Object.keys(reducers).forEach(function (key) {
  346. var reducer = reducers[key];
  347. var initialState = reducer(undefined, {
  348. type: ActionTypes.INIT
  349. });
  350. if (typeof initialState === 'undefined') {
  351. throw new Error("Reducer \"" + key + "\" returned undefined during initialization. " + "If the state passed to the reducer is undefined, you must " + "explicitly return the initial state. The initial state may " + "not be undefined. If you don't want to set a value for this reducer, " + "you can use null instead of undefined.");
  352. }
  353. if (typeof reducer(undefined, {
  354. type: ActionTypes.PROBE_UNKNOWN_ACTION()
  355. }) === 'undefined') {
  356. throw new Error("Reducer \"" + key + "\" returned undefined when probed with a random type. " + ("Don't try to handle " + ActionTypes.INIT + " or other actions in \"redux/*\" ") + "namespace. They are considered private. Instead, you must return the " + "current state for any unknown actions, unless it is undefined, " + "in which case you must return the initial state, regardless of the " + "action type. The initial state may not be undefined, but can be null.");
  357. }
  358. });
  359. }
  360. /**
  361. * Turns an object whose values are different reducer functions, into a single
  362. * reducer function. It will call every child reducer, and gather their results
  363. * into a single state object, whose keys correspond to the keys of the passed
  364. * reducer functions.
  365. *
  366. * @param {Object} reducers An object whose values correspond to different
  367. * reducer functions that need to be combined into one. One handy way to obtain
  368. * it is to use ES6 `import * as reducers` syntax. The reducers may never return
  369. * undefined for any action. Instead, they should return their initial state
  370. * if the state passed to them was undefined, and the current state for any
  371. * unrecognized action.
  372. *
  373. * @returns {Function} A reducer function that invokes every reducer inside the
  374. * passed object, and builds a state object with the same shape.
  375. */
  376. function combineReducers(reducers) {
  377. var reducerKeys = Object.keys(reducers);
  378. var finalReducers = {};
  379. for (var i = 0; i < reducerKeys.length; i++) {
  380. var key = reducerKeys[i];
  381. {
  382. if (typeof reducers[key] === 'undefined') {
  383. warning("No reducer provided for key \"" + key + "\"");
  384. }
  385. }
  386. if (typeof reducers[key] === 'function') {
  387. finalReducers[key] = reducers[key];
  388. }
  389. }
  390. var finalReducerKeys = Object.keys(finalReducers); // This is used to make sure we don't warn about the same
  391. // keys multiple times.
  392. var unexpectedKeyCache;
  393. {
  394. unexpectedKeyCache = {};
  395. }
  396. var shapeAssertionError;
  397. try {
  398. assertReducerShape(finalReducers);
  399. } catch (e) {
  400. shapeAssertionError = e;
  401. }
  402. return function combination(state, action) {
  403. if (state === void 0) {
  404. state = {};
  405. }
  406. if (shapeAssertionError) {
  407. throw shapeAssertionError;
  408. }
  409. {
  410. var warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache);
  411. if (warningMessage) {
  412. warning(warningMessage);
  413. }
  414. }
  415. var hasChanged = false;
  416. var nextState = {};
  417. for (var _i = 0; _i < finalReducerKeys.length; _i++) {
  418. var _key = finalReducerKeys[_i];
  419. var reducer = finalReducers[_key];
  420. var previousStateForKey = state[_key];
  421. var nextStateForKey = reducer(previousStateForKey, action);
  422. if (typeof nextStateForKey === 'undefined') {
  423. var errorMessage = getUndefinedStateErrorMessage(_key, action);
  424. throw new Error(errorMessage);
  425. }
  426. nextState[_key] = nextStateForKey;
  427. hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
  428. }
  429. hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length;
  430. return hasChanged ? nextState : state;
  431. };
  432. }
  433. function bindActionCreator(actionCreator, dispatch) {
  434. return function () {
  435. return dispatch(actionCreator.apply(this, arguments));
  436. };
  437. }
  438. /**
  439. * Turns an object whose values are action creators, into an object with the
  440. * same keys, but with every function wrapped into a `dispatch` call so they
  441. * may be invoked directly. This is just a convenience method, as you can call
  442. * `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
  443. *
  444. * For convenience, you can also pass an action creator as the first argument,
  445. * and get a dispatch wrapped function in return.
  446. *
  447. * @param {Function|Object} actionCreators An object whose values are action
  448. * creator functions. One handy way to obtain it is to use ES6 `import * as`
  449. * syntax. You may also pass a single function.
  450. *
  451. * @param {Function} dispatch The `dispatch` function available on your Redux
  452. * store.
  453. *
  454. * @returns {Function|Object} The object mimicking the original object, but with
  455. * every action creator wrapped into the `dispatch` call. If you passed a
  456. * function as `actionCreators`, the return value will also be a single
  457. * function.
  458. */
  459. function bindActionCreators(actionCreators, dispatch) {
  460. if (typeof actionCreators === 'function') {
  461. return bindActionCreator(actionCreators, dispatch);
  462. }
  463. if (typeof actionCreators !== 'object' || actionCreators === null) {
  464. throw new Error("bindActionCreators expected an object or a function, instead received " + (actionCreators === null ? 'null' : typeof actionCreators) + ". " + "Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?");
  465. }
  466. var boundActionCreators = {};
  467. for (var key in actionCreators) {
  468. var actionCreator = actionCreators[key];
  469. if (typeof actionCreator === 'function') {
  470. boundActionCreators[key] = bindActionCreator(actionCreator, dispatch);
  471. }
  472. }
  473. return boundActionCreators;
  474. }
  475. function _defineProperty(obj, key, value) {
  476. if (key in obj) {
  477. Object.defineProperty(obj, key, {
  478. value: value,
  479. enumerable: true,
  480. configurable: true,
  481. writable: true
  482. });
  483. } else {
  484. obj[key] = value;
  485. }
  486. return obj;
  487. }
  488. function ownKeys(object, enumerableOnly) {
  489. var keys = Object.keys(object);
  490. if (Object.getOwnPropertySymbols) {
  491. keys.push.apply(keys, Object.getOwnPropertySymbols(object));
  492. }
  493. if (enumerableOnly) keys = keys.filter(function (sym) {
  494. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  495. });
  496. return keys;
  497. }
  498. function _objectSpread2(target) {
  499. for (var i = 1; i < arguments.length; i++) {
  500. var source = arguments[i] != null ? arguments[i] : {};
  501. if (i % 2) {
  502. ownKeys(source, true).forEach(function (key) {
  503. _defineProperty(target, key, source[key]);
  504. });
  505. } else if (Object.getOwnPropertyDescriptors) {
  506. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  507. } else {
  508. ownKeys(source).forEach(function (key) {
  509. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  510. });
  511. }
  512. }
  513. return target;
  514. }
  515. /**
  516. * Composes single-argument functions from right to left. The rightmost
  517. * function can take multiple arguments as it provides the signature for
  518. * the resulting composite function.
  519. *
  520. * @param {...Function} funcs The functions to compose.
  521. * @returns {Function} A function obtained by composing the argument functions
  522. * from right to left. For example, compose(f, g, h) is identical to doing
  523. * (...args) => f(g(h(...args))).
  524. */
  525. function compose() {
  526. for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
  527. funcs[_key] = arguments[_key];
  528. }
  529. if (funcs.length === 0) {
  530. return function (arg) {
  531. return arg;
  532. };
  533. }
  534. if (funcs.length === 1) {
  535. return funcs[0];
  536. }
  537. return funcs.reduce(function (a, b) {
  538. return function () {
  539. return a(b.apply(void 0, arguments));
  540. };
  541. });
  542. }
  543. /**
  544. * Creates a store enhancer that applies middleware to the dispatch method
  545. * of the Redux store. This is handy for a variety of tasks, such as expressing
  546. * asynchronous actions in a concise manner, or logging every action payload.
  547. *
  548. * See `redux-thunk` package as an example of the Redux middleware.
  549. *
  550. * Because middleware is potentially asynchronous, this should be the first
  551. * store enhancer in the composition chain.
  552. *
  553. * Note that each middleware will be given the `dispatch` and `getState` functions
  554. * as named arguments.
  555. *
  556. * @param {...Function} middlewares The middleware chain to be applied.
  557. * @returns {Function} A store enhancer applying the middleware.
  558. */
  559. function applyMiddleware() {
  560. for (var _len = arguments.length, middlewares = new Array(_len), _key = 0; _key < _len; _key++) {
  561. middlewares[_key] = arguments[_key];
  562. }
  563. return function (createStore) {
  564. return function () {
  565. var store = createStore.apply(void 0, arguments);
  566. var _dispatch = function dispatch() {
  567. throw new Error('Dispatching while constructing your middleware is not allowed. ' + 'Other middleware would not be applied to this dispatch.');
  568. };
  569. var middlewareAPI = {
  570. getState: store.getState,
  571. dispatch: function dispatch() {
  572. return _dispatch.apply(void 0, arguments);
  573. }
  574. };
  575. var chain = middlewares.map(function (middleware) {
  576. return middleware(middlewareAPI);
  577. });
  578. _dispatch = compose.apply(void 0, chain)(store.dispatch);
  579. return _objectSpread2({}, store, {
  580. dispatch: _dispatch
  581. });
  582. };
  583. };
  584. }
  585. /*
  586. * This is a dummy function to check if the function name has been altered by minification.
  587. * If the function has been minified and NODE_ENV !== 'production', warn the user.
  588. */
  589. function isCrushed() {}
  590. if ( typeof isCrushed.name === 'string' && isCrushed.name !== 'isCrushed') {
  591. warning('You are currently using minified code outside of NODE_ENV === "production". ' + 'This means that you are running a slower development build of Redux. ' + 'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' + 'or setting mode to production in webpack (https://webpack.js.org/concepts/mode/) ' + 'to ensure you have the correct code for your production build.');
  592. }
  593. exports.__DO_NOT_USE__ActionTypes = ActionTypes;
  594. exports.applyMiddleware = applyMiddleware;
  595. exports.bindActionCreators = bindActionCreators;
  596. exports.combineReducers = combineReducers;
  597. exports.compose = compose;
  598. exports.createStore = createStore;
  599. Object.defineProperty(exports, '__esModule', { value: true });
  600. }));