implementing replaying flag to mark actions which are actually replayed

This commit is contained in:
Tomas Weiss 2016-04-01 16:10:45 +02:00
parent 600134b5d3
commit 7cd1deb8a0

View File

@ -59,7 +59,7 @@ const INIT_ACTION = { type: '@@INIT' };
/** /**
* Computes the next entry in the log by applying an action. * Computes the next entry in the log by applying an action.
*/ */
function computeNextEntry(reducer, action, state, error) { function computeNextEntry(reducer, action, state, error, replaying) {
if (error) { if (error) {
return { return {
state, state,
@ -70,7 +70,7 @@ function computeNextEntry(reducer, action, state, error) {
let nextState = state; let nextState = state;
let nextError; let nextError;
try { try {
nextState = reducer(state, action); nextState = reducer(state, action, replaying);
} catch (err) { } catch (err) {
nextError = err.toString(); nextError = err.toString();
if (typeof window === 'object' && typeof window.chrome !== 'undefined') { if (typeof window === 'object' && typeof window.chrome !== 'undefined') {
@ -97,7 +97,8 @@ function recomputeStates(
committedState, committedState,
actionsById, actionsById,
stagedActionIds, stagedActionIds,
skippedActionIds skippedActionIds,
replaying
) { ) {
// Optimization: exit early and return the same reference // Optimization: exit early and return the same reference
// if we know nothing could have changed. // if we know nothing could have changed.
@ -120,7 +121,7 @@ function recomputeStates(
const shouldSkip = skippedActionIds.indexOf(actionId) > -1; const shouldSkip = skippedActionIds.indexOf(actionId) > -1;
const entry = shouldSkip ? const entry = shouldSkip ?
previousEntry : previousEntry :
computeNextEntry(reducer, action, previousState, previousError); computeNextEntry(reducer, action, previousState, previousError, replaying);
nextComputedStates.push(entry); nextComputedStates.push(entry);
} }
@ -170,6 +171,10 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) {
// value whenever we feel like we don't have to recompute the states. // value whenever we feel like we don't have to recompute the states.
let minInvalidatedStateIndex = 0; let minInvalidatedStateIndex = 0;
// For now, potentially any action except PERFORM_ACTION is considered
// as replay
let replaying = true;
switch (liftedAction.type) { switch (liftedAction.type) {
case ActionTypes.RESET: { case ActionTypes.RESET: {
// Get back to the state the store was created with. // Get back to the state the store was created with.
@ -245,6 +250,10 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) {
stagedActionIds = [...stagedActionIds, actionId]; stagedActionIds = [...stagedActionIds, actionId];
// Optimization: we know that only the new action needs computing. // Optimization: we know that only the new action needs computing.
minInvalidatedStateIndex = stagedActionIds.length - 1; minInvalidatedStateIndex = stagedActionIds.length - 1;
// This is the first time Action is actually performed, therefore
// we don't consider this replay
replaying = false;
break; break;
} }
case ActionTypes.IMPORT_STATE: { case ActionTypes.IMPORT_STATE: {
@ -262,6 +271,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) {
break; break;
} }
case '@@redux/INIT': { case '@@redux/INIT': {
replaying = false;
// Always recompute states on hot reload and init. // Always recompute states on hot reload and init.
minInvalidatedStateIndex = 0; minInvalidatedStateIndex = 0;
break; break;
@ -281,7 +292,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) {
committedState, committedState,
actionsById, actionsById,
stagedActionIds, stagedActionIds,
skippedActionIds skippedActionIds,
replaying
); );
monitorState = monitorReducer(monitorState, liftedAction); monitorState = monitorReducer(monitorState, liftedAction);
return { return {