From 9c5b80edb4184a62955dd3a24f6a92b0f2da722a Mon Sep 17 00:00:00 2001 From: echenley Date: Tue, 9 Feb 2016 19:39:41 -0600 Subject: [PATCH] fix auto-commit on hot reload --- src/instrument.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/instrument.js b/src/instrument.js index ad57eae4..89f6b2ab 100644 --- a/src/instrument.js +++ b/src/instrument.js @@ -165,12 +165,21 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options computedStates } = liftedState; - function commitExcessActions() { + function commitExcessActions(excess) { // If maxAge has been exceeded, auto-commit excess. - const excess = stagedActionIds.length - options.maxAge + 1; - const idsToDelete = stagedActionIds.slice(1, excess + 1); + let 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); stagedActionIds = [0, ...stagedActionIds.slice(excess + 1)]; committedState = computedStates[excess].state; @@ -250,12 +259,9 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options break; } case ActionTypes.PERFORM_ACTION: { - if ( - options.maxAge && - stagedActionIds.length === options.maxAge && - !computedStates[1].error - ) { - commitExcessActions(); + // Auto-commit as new actions come in. + if (options.maxAge && stagedActionIds.length === options.maxAge) { + commitExcessActions(1); } if (currentStateIndex === stagedActionIds.length - 1) { @@ -288,8 +294,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options // Always recompute states on hot reload and init. minInvalidatedStateIndex = 0; - if (options.maxAge && stagedActionIds.length >= options.maxAge) { - // states must be computed prior to committing + if (options.maxAge && stagedActionIds.length > options.maxAge) { + // States must be recomputed before committing excess. computedStates = recomputeStates( computedStates, minInvalidatedStateIndex, @@ -299,7 +305,11 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options stagedActionIds, skippedActionIds ); - commitExcessActions(); + + commitExcessActions(stagedActionIds.length - options.maxAge); + + // Avoid double computation. + minInvalidatedStateIndex = Infinity; } break;