fix auto-commit on hot reload

This commit is contained in:
echenley 2016-02-09 19:39:41 -06:00
parent ef073a1fa4
commit 9c5b80edb4

View File

@ -165,12 +165,21 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
computedStates computedStates
} = liftedState; } = liftedState;
function commitExcessActions() { function commitExcessActions(excess) {
// If maxAge has been exceeded, auto-commit excess. // If maxAge has been exceeded, auto-commit excess.
const excess = stagedActionIds.length - options.maxAge + 1; let idsToDelete = stagedActionIds.slice(1, excess + 1);
const idsToDelete = stagedActionIds.slice(1, excess + 1);
for (let i = 0; i < idsToDelete.length; i++) {
if (computedStates[i + 1].error) {
// Stop if error is found. Commit only up to error.
excess = i;
idsToDelete = stagedActionIds.slice(1, excess + 1);
break;
} else {
delete actionsById[idsToDelete[i]];
}
}
idsToDelete.forEach(id => delete actionsById[id]);
skippedActionIds = skippedActionIds.filter(id => idsToDelete.indexOf(id) === -1); skippedActionIds = skippedActionIds.filter(id => idsToDelete.indexOf(id) === -1);
stagedActionIds = [0, ...stagedActionIds.slice(excess + 1)]; stagedActionIds = [0, ...stagedActionIds.slice(excess + 1)];
committedState = computedStates[excess].state; committedState = computedStates[excess].state;
@ -250,12 +259,9 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
break; break;
} }
case ActionTypes.PERFORM_ACTION: { case ActionTypes.PERFORM_ACTION: {
if ( // Auto-commit as new actions come in.
options.maxAge && if (options.maxAge && stagedActionIds.length === options.maxAge) {
stagedActionIds.length === options.maxAge && commitExcessActions(1);
!computedStates[1].error
) {
commitExcessActions();
} }
if (currentStateIndex === stagedActionIds.length - 1) { if (currentStateIndex === stagedActionIds.length - 1) {
@ -288,8 +294,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
// Always recompute states on hot reload and init. // Always recompute states on hot reload and init.
minInvalidatedStateIndex = 0; minInvalidatedStateIndex = 0;
if (options.maxAge && stagedActionIds.length >= options.maxAge) { if (options.maxAge && stagedActionIds.length > options.maxAge) {
// states must be computed prior to committing // States must be recomputed before committing excess.
computedStates = recomputeStates( computedStates = recomputeStates(
computedStates, computedStates,
minInvalidatedStateIndex, minInvalidatedStateIndex,
@ -299,7 +305,11 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
stagedActionIds, stagedActionIds,
skippedActionIds skippedActionIds
); );
commitExcessActions();
commitExcessActions(stagedActionIds.length - options.maxAge);
// Avoid double computation.
minInvalidatedStateIndex = Infinity;
} }
break; break;