2019-01-03 17:14:25 +03:00
|
|
|
import commitExcessActions from './commitExcessActions';
|
2020-10-26 02:32:04 +03:00
|
|
|
import { State } from '../reducers/instances';
|
|
|
|
import { Action } from 'redux';
|
2020-12-20 17:13:01 +03:00
|
|
|
import { PerformAction } from 'redux-devtools';
|
2019-01-03 17:14:25 +03:00
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
export function recompute(
|
2020-10-26 02:32:04 +03:00
|
|
|
previousLiftedState: State,
|
|
|
|
storeState: State,
|
|
|
|
action:
|
|
|
|
| PerformAction<Action<unknown>>
|
|
|
|
| { action: Action<unknown>; timestamp?: number; stack?: string },
|
2019-01-10 21:51:14 +03:00
|
|
|
nextActionId = 1,
|
2020-10-26 02:32:04 +03:00
|
|
|
maxAge?: number,
|
|
|
|
isExcess?: boolean
|
2019-01-10 21:51:14 +03:00
|
|
|
) {
|
2019-01-03 17:14:25 +03:00
|
|
|
const actionId = nextActionId - 1;
|
|
|
|
const liftedState = { ...previousLiftedState };
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
if (
|
|
|
|
liftedState.currentStateIndex ===
|
|
|
|
liftedState.stagedActionIds.length - 1
|
|
|
|
) {
|
2019-01-03 17:14:25 +03:00
|
|
|
liftedState.currentStateIndex++;
|
|
|
|
}
|
|
|
|
liftedState.stagedActionIds = [...liftedState.stagedActionIds, actionId];
|
|
|
|
liftedState.actionsById = { ...liftedState.actionsById };
|
2020-10-26 02:32:04 +03:00
|
|
|
if ((action as PerformAction<Action<unknown>>).type === 'PERFORM_ACTION') {
|
|
|
|
liftedState.actionsById[actionId] = action as PerformAction<
|
|
|
|
Action<unknown>
|
|
|
|
>;
|
2019-01-03 17:14:25 +03:00
|
|
|
} else {
|
|
|
|
liftedState.actionsById[actionId] = {
|
|
|
|
action: action.action || action,
|
|
|
|
timestamp: action.timestamp || Date.now(),
|
2019-01-04 00:02:41 +03:00
|
|
|
stack: action.stack,
|
2020-08-08 23:26:39 +03:00
|
|
|
type: 'PERFORM_ACTION',
|
2019-01-03 17:14:25 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
liftedState.nextActionId = nextActionId;
|
2019-01-10 21:51:14 +03:00
|
|
|
liftedState.computedStates = [
|
|
|
|
...liftedState.computedStates,
|
2020-08-08 23:26:39 +03:00
|
|
|
{ state: storeState },
|
2019-01-10 21:51:14 +03:00
|
|
|
];
|
2019-01-03 17:14:25 +03:00
|
|
|
|
|
|
|
if (isExcess) commitExcessActions(liftedState);
|
|
|
|
else if (maxAge) {
|
|
|
|
const excess = liftedState.stagedActionIds.length - maxAge;
|
|
|
|
if (excess > 0) commitExcessActions(liftedState, excess);
|
|
|
|
}
|
|
|
|
|
|
|
|
return liftedState;
|
|
|
|
}
|