redux-fluorine
Version:
A Redux enhancer to manage groups of actions as observables (agendas)
39 lines (31 loc) • 1.05 kB
JavaScript
import { RECOVER_AFTER_ERROR } from '../actions/filterActions';
import reapplyActionsWithout from './reapplyActionsWithout';
export default 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 === RECOVER_AFTER_ERROR) {
// Reapply all past actions subtracting the errored ones
var anchor = action.anchor;
var actions = action.actions;
return reapplyActionsWithout(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;
};
}