1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports['default'] = combineReducers;
|
5 |
|
6 | var _createStore = require('./createStore');
|
7 |
|
8 | var _isPlainObject = require('lodash/isPlainObject');
|
9 |
|
10 | var _isPlainObject2 = _interopRequireDefault(_isPlainObject);
|
11 |
|
12 | var _warning = require('./utils/warning');
|
13 |
|
14 | var _warning2 = _interopRequireDefault(_warning);
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
17 |
|
18 | function getUndefinedStateErrorMessage(key, action) {
|
19 | var actionType = action && action.type;
|
20 | var actionName = actionType && '"' + actionType.toString() + '"' || 'an action';
|
21 |
|
22 | return 'Given action ' + actionName + ', 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.';
|
23 | }
|
24 |
|
25 | function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
|
26 | var reducerKeys = Object.keys(reducers);
|
27 | var argumentName = action && action.type === _createStore.ActionTypes.INIT ? 'preloadedState argument passed to createStore' : 'previous state received by the reducer';
|
28 |
|
29 | if (reducerKeys.length === 0) {
|
30 | return 'Store does not have a valid reducer. Make sure the argument passed ' + 'to combineReducers is an object whose values are reducers.';
|
31 | }
|
32 |
|
33 | if (!(0, _isPlainObject2['default'])(inputState)) {
|
34 | 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('", "') + '"');
|
35 | }
|
36 |
|
37 | var unexpectedKeys = Object.keys(inputState).filter(function (key) {
|
38 | return !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key];
|
39 | });
|
40 |
|
41 | unexpectedKeys.forEach(function (key) {
|
42 | unexpectedKeyCache[key] = true;
|
43 | });
|
44 |
|
45 | if (unexpectedKeys.length > 0) {
|
46 | 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.');
|
47 | }
|
48 | }
|
49 |
|
50 | function assertReducerShape(reducers) {
|
51 | Object.keys(reducers).forEach(function (key) {
|
52 | var reducer = reducers[key];
|
53 | var initialState = reducer(undefined, { type: _createStore.ActionTypes.INIT });
|
54 |
|
55 | if (typeof initialState === 'undefined') {
|
56 | 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.');
|
57 | }
|
58 |
|
59 | var type = '@@redux/PROBE_UNKNOWN_ACTION_' + Math.random().toString(36).substring(7).split('').join('.');
|
60 | if (typeof reducer(undefined, { type: type }) === 'undefined') {
|
61 | throw new Error('Reducer "' + key + '" returned undefined when probed with a random type. ' + ('Don\'t try to handle ' + _createStore.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.');
|
62 | }
|
63 | });
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function combineReducers(reducers) {
|
83 | var reducerKeys = Object.keys(reducers);
|
84 | var finalReducers = {};
|
85 | for (var i = 0; i < reducerKeys.length; i++) {
|
86 | var key = reducerKeys[i];
|
87 |
|
88 | if (process.env.NODE_ENV !== 'production') {
|
89 | if (typeof reducers[key] === 'undefined') {
|
90 | (0, _warning2['default'])('No reducer provided for key "' + key + '"');
|
91 | }
|
92 | }
|
93 |
|
94 | if (typeof reducers[key] === 'function') {
|
95 | finalReducers[key] = reducers[key];
|
96 | }
|
97 | }
|
98 | var finalReducerKeys = Object.keys(finalReducers);
|
99 |
|
100 | var unexpectedKeyCache = void 0;
|
101 | if (process.env.NODE_ENV !== 'production') {
|
102 | unexpectedKeyCache = {};
|
103 | }
|
104 |
|
105 | var shapeAssertionError = void 0;
|
106 | try {
|
107 | assertReducerShape(finalReducers);
|
108 | } catch (e) {
|
109 | shapeAssertionError = e;
|
110 | }
|
111 |
|
112 | return function combination() {
|
113 | var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
114 | var action = arguments[1];
|
115 |
|
116 | if (shapeAssertionError) {
|
117 | throw shapeAssertionError;
|
118 | }
|
119 |
|
120 | if (process.env.NODE_ENV !== 'production') {
|
121 | var warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache);
|
122 | if (warningMessage) {
|
123 | (0, _warning2['default'])(warningMessage);
|
124 | }
|
125 | }
|
126 |
|
127 | var hasChanged = false;
|
128 | var nextState = {};
|
129 | for (var _i = 0; _i < finalReducerKeys.length; _i++) {
|
130 | var _key = finalReducerKeys[_i];
|
131 | var reducer = finalReducers[_key];
|
132 | var previousStateForKey = state[_key];
|
133 | var nextStateForKey = reducer(previousStateForKey, action);
|
134 | if (typeof nextStateForKey === 'undefined') {
|
135 | var errorMessage = getUndefinedStateErrorMessage(_key, action);
|
136 | throw new Error(errorMessage);
|
137 | }
|
138 | nextState[_key] = nextStateForKey;
|
139 | hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
|
140 | }
|
141 | return hasChanged ? nextState : state;
|
142 | };
|
143 | } |
\ | No newline at end of file |