Skip to content

Commit ef073a1

Browse files
committed
auto-commit on INIT, handle committing multiple
1 parent c883e83 commit ef073a1

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/instrument.js

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

168+
function commitExcessActions() {
169+
// If maxAge has been exceeded, auto-commit excess.
170+
const excess = stagedActionIds.length - options.maxAge + 1;
171+
const idsToDelete = stagedActionIds.slice(1, excess + 1);
172+
173+
idsToDelete.forEach(id => delete actionsById[id]);
174+
skippedActionIds = skippedActionIds.filter(id => idsToDelete.indexOf(id) === -1);
175+
stagedActionIds = [0, ...stagedActionIds.slice(excess + 1)];
176+
committedState = computedStates[excess].state;
177+
computedStates = computedStates.slice(excess);
178+
currentStateIndex = currentStateIndex > excess
179+
? currentStateIndex - excess
180+
: 0;
181+
}
182+
168183
// By default, agressively recompute every state whatever happens.
169184
// This has O(n) performance, so we'll override this to a sensible
170185
// value whenever we feel like we don't have to recompute the states.
@@ -240,13 +255,10 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
240255
stagedActionIds.length === options.maxAge &&
241256
!computedStates[1].error
242257
) {
243-
// If maxAge has been reached, auto-commit earliest non-@@INIT action.
244-
delete actionsById[stagedActionIds[1]];
245-
skippedActionIds = skippedActionIds.filter(id => id !== stagedActionIds[1]);
246-
stagedActionIds = [0].concat(stagedActionIds.slice(2));
247-
committedState = computedStates[1].state;
248-
computedStates = computedStates.slice(1);
249-
} else if (currentStateIndex === stagedActionIds.length - 1) {
258+
commitExcessActions();
259+
}
260+
261+
if (currentStateIndex === stagedActionIds.length - 1) {
250262
currentStateIndex++;
251263
}
252264
const actionId = nextActionId++;
@@ -275,6 +287,21 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
275287
case '@@redux/INIT': {
276288
// Always recompute states on hot reload and init.
277289
minInvalidatedStateIndex = 0;
290+
291+
if (options.maxAge && stagedActionIds.length >= options.maxAge) {
292+
// states must be computed prior to committing
293+
computedStates = recomputeStates(
294+
computedStates,
295+
minInvalidatedStateIndex,
296+
reducer,
297+
committedState,
298+
actionsById,
299+
stagedActionIds,
300+
skippedActionIds
301+
);
302+
commitExcessActions();
303+
}
304+
278305
break;
279306
}
280307
default: {

0 commit comments

Comments
 (0)