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