UNPKG

3.63 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.default = joinReducers;
5
6var _lodash = require('lodash.isfunction');
7
8var _lodash2 = _interopRequireDefault(_lodash);
9
10var _lodash3 = require('lodash.last');
11
12var _lodash4 = _interopRequireDefault(_lodash3);
13
14var _verify = require('../util/verify');
15
16var _verify2 = _interopRequireDefault(_verify);
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20/**
21 * Create a higher-order reducer by combining two or more reducers,
22 * logically executing each in sequence (in essence combining their
23 * functionality into one). This is useful when combining various
24 * reducer types into one logical construct.
25 *
26 * **Please Note:** Because each reducer is able to build on what has
27 * been accomplished by a prior reducer, joinReducers cumulatively
28 * passes the state parameter that was returned from any prior reducer
29 * (in the chain of reducers to execute). In essence this is an
30 * accumulative process. While this does NOT relax the immutable
31 * constraint of the reducer's state parameter, it is possible for a
32 * down-stream reducer to receive a state parameter that is a
33 * different instance from the start of the reduction process (because
34 * an up-stream reducer needed to alter it in some way).
35 *
36 * The {{book.guide.devGuide}} discusses joinReducers() in more detail
37 * (see {{book.guide.conceptJoin}}), and additional examples can
38 * be found in {{book.guide.fullExample}}.
39 *
40 * @param {...reducerFn} reducerFns two or more reducer functions to join
41 * together.
42 *
43 * @param {InitialState} [initialState] - the optional fall-back state
44 * value used during the state initialization boot-strap process.
45 *
46 * @returns {reducerFn} a newly created reducer function (described above).
47 */
48function joinReducers() {
49 for (var _len = arguments.length, reducerFns = Array(_len), _key = 0; _key < _len; _key++) {
50 reducerFns[_key] = arguments[_key];
51 }
52
53 // define our initialState parameter (optionally, the last parameter)
54 // NOTE: We have to do this programatically because our function
55 // accepts variable number of arguments.
56 var initialState = (0, _lodash2.default)((0, _lodash4.default)(reducerFns)) ? undefined : reducerFns.pop();
57
58 // validate params
59 var check = _verify2.default.prefix('AstxReduxUtil.joinReducers() parameter violation: ');
60
61 check(reducerFns && reducerFns.length >= 2, 'two or more reducerFn arguments are required');
62
63 // ... each arg MUST be a function (reducerFn)
64 var badArgNum = reducerFns.reduce(function (firstBadArgNum, reducerFn, indx) {
65 return firstBadArgNum || ((0, _lodash2.default)(reducerFn) ? 0 : indx + 1);
66 }, 0);
67 check(!badArgNum, 'argument position number ' + badArgNum + ' is NOT a function ... expecting two or more reducerFns to join together');
68
69 // expose our new higher-order reducer
70 return function () {
71 var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
72 var action = arguments[1];
73 var originalReducerState = arguments[2];
74
75
76 // maintain the originalReducerState as the immutable state
77 // at the time of the start of the reduction process
78 // ... in support of joinReducers()
79 // ... for more info, refer to the Dev Guide {{book.guide.originalReducerState}}
80 if (originalReducerState === undefined) {
81 originalReducerState = state;
82 }
83
84 // execute each reducerFn in sequence
85 return reducerFns.reduce(function (nextState, reducerFn) {
86 return reducerFn(nextState, action, originalReducerState);
87 }, state);
88 };
89}
\No newline at end of file