UNPKG

5.58 kBJavaScriptView Raw
1import { ActionTypes } from './createStore';
2import isPlainObject from 'lodash-es/isPlainObject';
3import warning from './utils/warning';
4
5function getUndefinedStateErrorMessage(key, action) {
6 var actionType = action && action.type;
7 var actionName = actionType && '"' + actionType.toString() + '"' || 'an action';
8
9 return 'Given action ' + actionName + ', reducer "' + key + '" returned undefined. ' + 'To ignore an action, you must explicitly return the previous state.';
10}
11
12function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
13 var reducerKeys = Object.keys(reducers);
14 var argumentName = action && action.type === ActionTypes.INIT ? 'preloadedState argument passed to createStore' : 'previous state received by the reducer';
15
16 if (reducerKeys.length === 0) {
17 return 'Store does not have a valid reducer. Make sure the argument passed ' + 'to combineReducers is an object whose values are reducers.';
18 }
19
20 if (!isPlainObject(inputState)) {
21 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('", "') + '"');
22 }
23
24 var unexpectedKeys = Object.keys(inputState).filter(function (key) {
25 return !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key];
26 });
27
28 unexpectedKeys.forEach(function (key) {
29 unexpectedKeyCache[key] = true;
30 });
31
32 if (unexpectedKeys.length > 0) {
33 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.');
34 }
35}
36
37function assertReducerSanity(reducers) {
38 Object.keys(reducers).forEach(function (key) {
39 var reducer = reducers[key];
40 var initialState = reducer(undefined, { type: ActionTypes.INIT });
41
42 if (typeof initialState === 'undefined') {
43 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.');
44 }
45
46 var type = '@@redux/PROBE_UNKNOWN_ACTION_' + Math.random().toString(36).substring(7).split('').join('.');
47 if (typeof reducer(undefined, { type: type }) === 'undefined') {
48 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.');
49 }
50 });
51}
52
53/**
54 * Turns an object whose values are different reducer functions, into a single
55 * reducer function. It will call every child reducer, and gather their results
56 * into a single state object, whose keys correspond to the keys of the passed
57 * reducer functions.
58 *
59 * @param {Object} reducers An object whose values correspond to different
60 * reducer functions that need to be combined into one. One handy way to obtain
61 * it is to use ES6 `import * as reducers` syntax. The reducers may never return
62 * undefined for any action. Instead, they should return their initial state
63 * if the state passed to them was undefined, and the current state for any
64 * unrecognized action.
65 *
66 * @returns {Function} A reducer function that invokes every reducer inside the
67 * passed object, and builds a state object with the same shape.
68 */
69export default function combineReducers(reducers) {
70 var reducerKeys = Object.keys(reducers);
71 var finalReducers = {};
72 for (var i = 0; i < reducerKeys.length; i++) {
73 var key = reducerKeys[i];
74
75 if (process.env.NODE_ENV !== 'production') {
76 if (typeof reducers[key] === 'undefined') {
77 warning('No reducer provided for key "' + key + '"');
78 }
79 }
80
81 if (typeof reducers[key] === 'function') {
82 finalReducers[key] = reducers[key];
83 }
84 }
85 var finalReducerKeys = Object.keys(finalReducers);
86
87 if (process.env.NODE_ENV !== 'production') {
88 var unexpectedKeyCache = {};
89 }
90
91 var sanityError;
92 try {
93 assertReducerSanity(finalReducers);
94 } catch (e) {
95 sanityError = e;
96 }
97
98 return function combination() {
99 var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
100 var action = arguments[1];
101
102 if (sanityError) {
103 throw sanityError;
104 }
105
106 if (process.env.NODE_ENV !== 'production') {
107 var warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache);
108 if (warningMessage) {
109 warning(warningMessage);
110 }
111 }
112
113 var hasChanged = false;
114 var nextState = {};
115 for (var i = 0; i < finalReducerKeys.length; i++) {
116 var key = finalReducerKeys[i];
117 var reducer = finalReducers[key];
118 var previousStateForKey = state[key];
119 var nextStateForKey = reducer(previousStateForKey, action);
120 if (typeof nextStateForKey === 'undefined') {
121 var errorMessage = getUndefinedStateErrorMessage(key, action);
122 throw new Error(errorMessage);
123 }
124 nextState[key] = nextStateForKey;
125 hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
126 }
127 return hasChanged ? nextState : state;
128 };
129}
\No newline at end of file