redux-fluorine
Version:
A Redux enhancer to manage groups of actions as observables (agendas)
51 lines (38 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = wrapStateReducer;
var _filterActions = require('../actions/filterActions');
var _reapplyActionsWithout = require('./reapplyActionsWithout');
var _reapplyActionsWithout2 = _interopRequireDefault(_reapplyActionsWithout);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function wrapStateReducer(reducer, initialState) {
if (typeof reducer !== 'function') {
throw new Error('wrapStateReducer: Expected `reducer` to be a function.');
}
var _defaultState = {
action: null,
state: initialState,
next: null
};
return function () {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _defaultState;
var action = arguments[1];
if (action.type === _filterActions.RECOVER_AFTER_ERROR) {
// Reapply all past actions subtracting the errored ones
var anchor = action.anchor;
var actions = action.actions;
return (0, _reapplyActionsWithout2.default)(reducer, anchor, actions);
}
var lastState = state.state;
var nextState = reducer(lastState, action);
// Create next wrapped state and link it
state.next = {
action: action,
state: nextState,
next: null
};
return state.next;
};
}