redux.js 27 KB

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