redux-devtools/packages/redux-devtools-app/src/utils/commitExcessActions.ts
Nathan Bierema 0399d306c0
chore(*): move redux-devtools-app to scoped package (#693)
* stash

* more

* cli rename

* Remove reference

* Fix another reference

* Fix scripts

* Fix package name

* Fix tsconfig
2020-12-22 12:02:14 -05:00

36 lines
1.2 KiB
TypeScript

// Based on https://github.com/gaearon/redux-devtools/pull/241
/* eslint-disable no-param-reassign */
import { State } from '../reducers/instances';
export default function commitExcessActions(liftedState: State, n = 1) {
// Auto-commits n-number of excess actions.
let excess = n;
let idsToDelete = liftedState.stagedActionIds.slice(1, excess + 1);
for (let i = 0; i < idsToDelete.length; i++) {
if (liftedState.computedStates[i + 1].error) {
// Stop if error is found. Commit actions up to error.
excess = i;
idsToDelete = liftedState.stagedActionIds.slice(1, excess + 1);
break;
} else {
delete liftedState.actionsById[idsToDelete[i]];
}
}
liftedState.skippedActionIds = liftedState.skippedActionIds.filter(
(id) => idsToDelete.indexOf(id) === -1
);
liftedState.stagedActionIds = [
0,
...liftedState.stagedActionIds.slice(excess + 1),
];
liftedState.committedState = liftedState.computedStates[excess].state;
liftedState.computedStates = liftedState.computedStates.slice(excess);
liftedState.currentStateIndex =
liftedState.currentStateIndex > excess
? liftedState.currentStateIndex - excess
: 0;
}