Skip to content

Commit 9c5b80e

Browse files
committed
fix auto-commit on hot reload
1 parent ef073a1 commit 9c5b80e

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/instrument.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,21 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
165165
computedStates
166166
} = liftedState;
167167

168-
function commitExcessActions() {
168+
function commitExcessActions(excess) {
169169
// If maxAge has been exceeded, auto-commit excess.
170-
const excess = stagedActionIds.length - options.maxAge + 1;
171-
const idsToDelete = stagedActionIds.slice(1, excess + 1);
170+
let idsToDelete = stagedActionIds.slice(1, excess + 1);
171+
172+
for (let i = 0; i < idsToDelete.length; i++) {
173+
if (computedStates[i + 1].error) {
174+
// Stop if error is found. Commit only up to error.
175+
excess = i;
176+
idsToDelete = stagedActionIds.slice(1, excess + 1);
177+
break;
178+
} else {
179+
delete actionsById[idsToDelete[i]];
180+
}
181+
}
172182

173-
idsToDelete.forEach(id => delete actionsById[id]);
174183
skippedActionIds = skippedActionIds.filter(id => idsToDelete.indexOf(id) === -1);
175184
stagedActionIds = [0, ...stagedActionIds.slice(excess + 1)];
176185
committedState = computedStates[excess].state;
@@ -250,12 +259,9 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
250259
break;
251260
}
252261
case ActionTypes.PERFORM_ACTION: {
253-
if (
254-
options.maxAge &&
255-
stagedActionIds.length === options.maxAge &&
256-
!computedStates[1].error
257-
) {
258-
commitExcessActions();
262+
// Auto-commit as new actions come in.
263+
if (options.maxAge && stagedActionIds.length === options.maxAge) {
264+
commitExcessActions(1);
259265
}
260266

261267
if (currentStateIndex === stagedActionIds.length - 1) {
@@ -288,8 +294,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
288294
// Always recompute states on hot reload and init.
289295
minInvalidatedStateIndex = 0;
290296

291-
if (options.maxAge && stagedActionIds.length >= options.maxAge) {
292-
// states must be computed prior to committing
297+
if (options.maxAge && stagedActionIds.length > options.maxAge) {
298+
// States must be recomputed before committing excess.
293299
computedStates = recomputeStates(
294300
computedStates,
295301
minInvalidatedStateIndex,
@@ -299,7 +305,11 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
299305
stagedActionIds,
300306
skippedActionIds
301307
);
302-
commitExcessActions();
308+
309+
commitExcessActions(stagedActionIds.length - options.maxAge);
310+
311+
// Avoid double computation.
312+
minInvalidatedStateIndex = Infinity;
303313
}
304314

305315
break;

0 commit comments

Comments
 (0)