redux.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. * @deprecated
  97. *
  98. * **We recommend using the `configureStore` method
  99. * of the `@reduxjs/toolkit` package**, which replaces `createStore`.
  100. *
  101. * Redux Toolkit is our recommended approach for writing Redux logic today,
  102. * including store setup, reducers, data fetching, and more.
  103. *
  104. * **For more details, please read this Redux docs page:**
  105. * **https://redux.js.org/introduction/why-rtk-is-redux-today**
  106. *
  107. * `configureStore` from Redux Toolkit is an improved version of `createStore` that
  108. * simplifies setup and helps avoid common bugs.
  109. *
  110. * You should not be using the `redux` core package by itself today, except for learning purposes.
  111. * The `createStore` method from the core `redux` package will not be removed, but we encourage
  112. * all users to migrate to using Redux Toolkit for all Redux code.
  113. *
  114. * If you want to use `createStore` without this visual deprecation warning, use
  115. * the `legacy_createStore` import instead:
  116. *
  117. * `import { legacy_createStore as createStore} from 'redux'`
  118. *
  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. * Creates a Redux store that holds the state tree.
  334. *
  335. * **We recommend using `configureStore` from the
  336. * `@reduxjs/toolkit` package**, which replaces `createStore`:
  337. * **https://redux.js.org/introduction/why-rtk-is-redux-today**
  338. *
  339. * The only way to change the data in the store is to call `dispatch()` on it.
  340. *
  341. * There should only be a single store in your app. To specify how different
  342. * parts of the state tree respond to actions, you may combine several reducers
  343. * into a single reducer function by using `combineReducers`.
  344. *
  345. * @param {Function} reducer A function that returns the next state tree, given
  346. * the current state tree and the action to handle.
  347. *
  348. * @param {any} [preloadedState] The initial state. You may optionally specify it
  349. * to hydrate the state from the server in universal apps, or to restore a
  350. * previously serialized user session.
  351. * If you use `combineReducers` to produce the root reducer function, this must be
  352. * an object with the same shape as `combineReducers` keys.
  353. *
  354. * @param {Function} [enhancer] The store enhancer. You may optionally specify it
  355. * to enhance the store with third-party capabilities such as middleware,
  356. * time travel, persistence, etc. The only store enhancer that ships with Redux
  357. * is `applyMiddleware()`.
  358. *
  359. * @returns {Store} A Redux store that lets you read the state, dispatch actions
  360. * and subscribe to changes.
  361. */
  362. var legacy_createStore = createStore;
  363. /**
  364. * Prints a warning in the console if it exists.
  365. *
  366. * @param {String} message The warning message.
  367. * @returns {void}
  368. */
  369. function warning(message) {
  370. /* eslint-disable no-console */
  371. if (typeof console !== 'undefined' && typeof console.error === 'function') {
  372. console.error(message);
  373. }
  374. /* eslint-enable no-console */
  375. try {
  376. // This error was thrown as a convenience so that if you enable
  377. // "break on all exceptions" in your console,
  378. // it would pause the execution at this line.
  379. throw new Error(message);
  380. } catch (e) {} // eslint-disable-line no-empty
  381. }
  382. function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
  383. var reducerKeys = Object.keys(reducers);
  384. var argumentName = action && action.type === ActionTypes.INIT ? 'preloadedState argument passed to createStore' : 'previous state received by the reducer';
  385. if (reducerKeys.length === 0) {
  386. return 'Store does not have a valid reducer. Make sure the argument passed ' + 'to combineReducers is an object whose values are reducers.';
  387. }
  388. if (!isPlainObject(inputState)) {
  389. return "The " + argumentName + " has unexpected type of \"" + kindOf(inputState) + "\". Expected argument to be an object with the following " + ("keys: \"" + reducerKeys.join('", "') + "\"");
  390. }
  391. var unexpectedKeys = Object.keys(inputState).filter(function (key) {
  392. return !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key];
  393. });
  394. unexpectedKeys.forEach(function (key) {
  395. unexpectedKeyCache[key] = true;
  396. });
  397. if (action && action.type === ActionTypes.REPLACE) return;
  398. if (unexpectedKeys.length > 0) {
  399. 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.");
  400. }
  401. }
  402. function assertReducerShape(reducers) {
  403. Object.keys(reducers).forEach(function (key) {
  404. var reducer = reducers[key];
  405. var initialState = reducer(undefined, {
  406. type: ActionTypes.INIT
  407. });
  408. if (typeof initialState === 'undefined') {
  409. 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.");
  410. }
  411. if (typeof reducer(undefined, {
  412. type: ActionTypes.PROBE_UNKNOWN_ACTION()
  413. }) === 'undefined') {
  414. 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.");
  415. }
  416. });
  417. }
  418. /**
  419. * Turns an object whose values are different reducer functions, into a single
  420. * reducer function. It will call every child reducer, and gather their results
  421. * into a single state object, whose keys correspond to the keys of the passed
  422. * reducer functions.
  423. *
  424. * @param {Object} reducers An object whose values correspond to different
  425. * reducer functions that need to be combined into one. One handy way to obtain
  426. * it is to use ES6 `import * as reducers` syntax. The reducers may never return
  427. * undefined for any action. Instead, they should return their initial state
  428. * if the state passed to them was undefined, and the current state for any
  429. * unrecognized action.
  430. *
  431. * @returns {Function} A reducer function that invokes every reducer inside the
  432. * passed object, and builds a state object with the same shape.
  433. */
  434. function combineReducers(reducers) {
  435. var reducerKeys = Object.keys(reducers);
  436. var finalReducers = {};
  437. for (var i = 0; i < reducerKeys.length; i++) {
  438. var key = reducerKeys[i];
  439. if (process.env.NODE_ENV !== 'production') {
  440. if (typeof reducers[key] === 'undefined') {
  441. warning("No reducer provided for key \"" + key + "\"");
  442. }
  443. }
  444. if (typeof reducers[key] === 'function') {
  445. finalReducers[key] = reducers[key];
  446. }
  447. }
  448. var finalReducerKeys = Object.keys(finalReducers); // This is used to make sure we don't warn about the same
  449. // keys multiple times.
  450. var unexpectedKeyCache;
  451. if (process.env.NODE_ENV !== 'production') {
  452. unexpectedKeyCache = {};
  453. }
  454. var shapeAssertionError;
  455. try {
  456. assertReducerShape(finalReducers);
  457. } catch (e) {
  458. shapeAssertionError = e;
  459. }
  460. return function combination(state, action) {
  461. if (state === void 0) {
  462. state = {};
  463. }
  464. if (shapeAssertionError) {
  465. throw shapeAssertionError;
  466. }
  467. if (process.env.NODE_ENV !== 'production') {
  468. var warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache);
  469. if (warningMessage) {
  470. warning(warningMessage);
  471. }
  472. }
  473. var hasChanged = false;
  474. var nextState = {};
  475. for (var _i = 0; _i < finalReducerKeys.length; _i++) {
  476. var _key = finalReducerKeys[_i];
  477. var reducer = finalReducers[_key];
  478. var previousStateForKey = state[_key];
  479. var nextStateForKey = reducer(previousStateForKey, action);
  480. if (typeof nextStateForKey === 'undefined') {
  481. var actionType = action && action.type;
  482. 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.");
  483. }
  484. nextState[_key] = nextStateForKey;
  485. hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
  486. }
  487. hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length;
  488. return hasChanged ? nextState : state;
  489. };
  490. }
  491. function bindActionCreator(actionCreator, dispatch) {
  492. return function () {
  493. return dispatch(actionCreator.apply(this, arguments));
  494. };
  495. }
  496. /**
  497. * Turns an object whose values are action creators, into an object with the
  498. * same keys, but with every function wrapped into a `dispatch` call so they
  499. * may be invoked directly. This is just a convenience method, as you can call
  500. * `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
  501. *
  502. * For convenience, you can also pass an action creator as the first argument,
  503. * and get a dispatch wrapped function in return.
  504. *
  505. * @param {Function|Object} actionCreators An object whose values are action
  506. * creator functions. One handy way to obtain it is to use ES6 `import * as`
  507. * syntax. You may also pass a single function.
  508. *
  509. * @param {Function} dispatch The `dispatch` function available on your Redux
  510. * store.
  511. *
  512. * @returns {Function|Object} The object mimicking the original object, but with
  513. * every action creator wrapped into the `dispatch` call. If you passed a
  514. * function as `actionCreators`, the return value will also be a single
  515. * function.
  516. */
  517. function bindActionCreators(actionCreators, dispatch) {
  518. if (typeof actionCreators === 'function') {
  519. return bindActionCreator(actionCreators, dispatch);
  520. }
  521. if (typeof actionCreators !== 'object' || actionCreators === null) {
  522. 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\"?");
  523. }
  524. var boundActionCreators = {};
  525. for (var key in actionCreators) {
  526. var actionCreator = actionCreators[key];
  527. if (typeof actionCreator === 'function') {
  528. boundActionCreators[key] = bindActionCreator(actionCreator, dispatch);
  529. }
  530. }
  531. return boundActionCreators;
  532. }
  533. /**
  534. * Composes single-argument functions from right to left. The rightmost
  535. * function can take multiple arguments as it provides the signature for
  536. * the resulting composite function.
  537. *
  538. * @param {...Function} funcs The functions to compose.
  539. * @returns {Function} A function obtained by composing the argument functions
  540. * from right to left. For example, compose(f, g, h) is identical to doing
  541. * (...args) => f(g(h(...args))).
  542. */
  543. function compose() {
  544. for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
  545. funcs[_key] = arguments[_key];
  546. }
  547. if (funcs.length === 0) {
  548. return function (arg) {
  549. return arg;
  550. };
  551. }
  552. if (funcs.length === 1) {
  553. return funcs[0];
  554. }
  555. return funcs.reduce(function (a, b) {
  556. return function () {
  557. return a(b.apply(void 0, arguments));
  558. };
  559. });
  560. }
  561. /**
  562. * Creates a store enhancer that applies middleware to the dispatch method
  563. * of the Redux store. This is handy for a variety of tasks, such as expressing
  564. * asynchronous actions in a concise manner, or logging every action payload.
  565. *
  566. * See `redux-thunk` package as an example of the Redux middleware.
  567. *
  568. * Because middleware is potentially asynchronous, this should be the first
  569. * store enhancer in the composition chain.
  570. *
  571. * Note that each middleware will be given the `dispatch` and `getState` functions
  572. * as named arguments.
  573. *
  574. * @param {...Function} middlewares The middleware chain to be applied.
  575. * @returns {Function} A store enhancer applying the middleware.
  576. */
  577. function applyMiddleware() {
  578. for (var _len = arguments.length, middlewares = new Array(_len), _key = 0; _key < _len; _key++) {
  579. middlewares[_key] = arguments[_key];
  580. }
  581. return function (createStore) {
  582. return function () {
  583. var store = createStore.apply(void 0, arguments);
  584. var _dispatch = function dispatch() {
  585. 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.');
  586. };
  587. var middlewareAPI = {
  588. getState: store.getState,
  589. dispatch: function dispatch() {
  590. return _dispatch.apply(void 0, arguments);
  591. }
  592. };
  593. var chain = middlewares.map(function (middleware) {
  594. return middleware(middlewareAPI);
  595. });
  596. _dispatch = compose.apply(void 0, chain)(store.dispatch);
  597. return _objectSpread__default['default'](_objectSpread__default['default']({}, store), {}, {
  598. dispatch: _dispatch
  599. });
  600. };
  601. };
  602. }
  603. exports.__DO_NOT_USE__ActionTypes = ActionTypes;
  604. exports.applyMiddleware = applyMiddleware;
  605. exports.bindActionCreators = bindActionCreators;
  606. exports.combineReducers = combineReducers;
  607. exports.compose = compose;
  608. exports.createStore = createStore;
  609. exports.legacy_createStore = legacy_createStore;