1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = conditionalReducer;
|
7 |
|
8 | var _lodash = require('lodash.identity');
|
9 |
|
10 | var _lodash2 = _interopRequireDefault(_lodash);
|
11 |
|
12 | var _lodash3 = require('lodash.isfunction');
|
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 that conditionally executes one of
|
24 | * the supplied reducerFns, based on the conditionalFn() return
|
25 | * directive.
|
26 | *
|
27 | * The {{book.guide.devGuide}} discusses conditionalReducer() in more detail
|
28 | * (see {{book.guide.conceptConditional}}), and additional examples can
|
29 | * be found in {{book.guide.conceptJoin}} and {{book.guide.fullExample}}.
|
30 | *
|
31 | * @param {conditionalReducerCB} conditionalFn - a callback function
|
32 | * whose return value determines which reducerFn is executed
|
33 | * ... truthy: thenReducerFn(), falsy: elseReducerFn().
|
34 | *
|
35 | * @param {reducerFn} thenReducerFn - the "wrapped" reducer invoked
|
36 | * when conditionalFn returns truthy.
|
37 | *
|
38 | * @param {reducerFn} [elseReducerFn=identity] - the
|
39 | * optional "wrapped" reducer invoked when conditionalFn returns
|
40 | * falsy. DEFAULT: [identity function](https://lodash.com/docs#identity)
|
41 | *
|
42 | * @param {InitialState} [initialState] - the optional fall-back state
|
43 | * value used during the state initialization boot-strap process.
|
44 | *
|
45 | * @returns {reducerFn} a newly created reducer function (described above).
|
46 | */
|
47 | function conditionalReducer(conditionalFn, thenReducerFn) {
|
48 | var elseReducerFn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _lodash2.default;
|
49 | var initialState = arguments[3];
|
50 |
|
51 |
|
52 | // validate params
|
53 | var check = _verify2.default.prefix('AstxReduxUtil.conditionalReducer() parameter violation: ');
|
54 |
|
55 | check(conditionalFn, 'conditionalFn argument is required');
|
56 | check((0, _lodash4.default)(conditionalFn), 'conditionalFn argument is NOT a function');
|
57 | check(thenReducerFn, 'thenReducerFn argument is required');
|
58 | check((0, _lodash4.default)(thenReducerFn), 'thenReducerFn argument is NOT a function');
|
59 | check((0, _lodash4.default)(elseReducerFn), 'elseReducerFn argument is NOT a function');
|
60 |
|
61 | // expose our new higher-order reducer
|
62 | // NOTE: For more info on he originalReducerState parameter, refer to the Dev Guide {{book.guide.originalReducerState}}
|
63 | return function () {
|
64 | var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
|
65 | var action = arguments[1];
|
66 | var originalReducerState = arguments[2];
|
67 |
|
68 |
|
69 | // maintain the originalReducerState as the immutable state
|
70 | // at the time of the start of the reduction process
|
71 | // ... in support of joinReducers()
|
72 | // ... for more info, refer to the Dev Guide {{book.guide.originalReducerState}}
|
73 | if (originalReducerState === undefined) {
|
74 | originalReducerState = state;
|
75 | }
|
76 |
|
77 | // execute either thenReducerFn or elseReducerFn, based on conditionalFn
|
78 | return conditionalFn(state, action, originalReducerState) ? thenReducerFn(state, action, originalReducerState) : elseReducerFn(state, action, originalReducerState);
|
79 | };
|
80 | }
|
81 |
|
82 | //***
|
83 | //*** Specification: conditionalReducerCB
|
84 | //***
|
85 |
|
86 | /**
|
87 | * A callback function (used in {{book.api.conditionalReducer}}) whose
|
88 | * return value determines which reducerFn is executed.
|
89 | *
|
90 | * @callback conditionalReducerCB
|
91 | *
|
92 | * @param {*} state - The current immutable state that is the
|
93 | * reduction target.
|
94 | *
|
95 | * @param {Action} action - The standard redux Action object that
|
96 | * drives the reduction process.
|
97 | *
|
98 | * @param {*} originalReducerState - The immutable state at the time
|
99 | * of the start of the reduction process.
|
100 | *
|
101 | * This is useful in determining whether state has changed within a
|
102 | * series of reductions {{book.api.joinReducers}} ... because each
|
103 | * individual reducer only has visibility of the state within it's own
|
104 | * reduction process.
|
105 | *
|
106 | * Further information can be found in the
|
107 | * {{book.guide.originalReducerState}} discussion of the {{book.guide.devGuide}}.
|
108 | *
|
109 | * @returns {truthy} A truthy value indicating which reducerFn is
|
110 | * executed ... truthy: thenReducerFn(), falsy: elseReducerFn().
|
111 | */ |
\ | No newline at end of file |