From 7cd1deb8a0be05e5aaffa339cc9e43ba82914831 Mon Sep 17 00:00:00 2001 From: Tomas Weiss Date: Fri, 1 Apr 2016 16:10:45 +0200 Subject: [PATCH] implementing replaying flag to mark actions which are actually replayed --- src/instrument.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/instrument.js b/src/instrument.js index 3b6d5678..04a4745a 100644 --- a/src/instrument.js +++ b/src/instrument.js @@ -59,7 +59,7 @@ const INIT_ACTION = { type: '@@INIT' }; /** * 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) { return { state, @@ -70,7 +70,7 @@ function computeNextEntry(reducer, action, state, error) { let nextState = state; let nextError; try { - nextState = reducer(state, action); + nextState = reducer(state, action, replaying); } catch (err) { nextError = err.toString(); if (typeof window === 'object' && typeof window.chrome !== 'undefined') { @@ -97,7 +97,8 @@ function recomputeStates( committedState, actionsById, stagedActionIds, - skippedActionIds + skippedActionIds, + replaying ) { // Optimization: exit early and return the same reference // if we know nothing could have changed. @@ -120,7 +121,7 @@ function recomputeStates( const shouldSkip = skippedActionIds.indexOf(actionId) > -1; const entry = shouldSkip ? previousEntry : - computeNextEntry(reducer, action, previousState, previousError); + computeNextEntry(reducer, action, previousState, previousError, replaying); 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. let minInvalidatedStateIndex = 0; + // For now, potentially any action except PERFORM_ACTION is considered + // as replay + let replaying = true; + switch (liftedAction.type) { case ActionTypes.RESET: { // Get back to the state the store was created with. @@ -245,6 +250,10 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) { stagedActionIds = [...stagedActionIds, actionId]; // Optimization: we know that only the new action needs computing. minInvalidatedStateIndex = stagedActionIds.length - 1; + + // This is the first time Action is actually performed, therefore + // we don't consider this replay + replaying = false; break; } case ActionTypes.IMPORT_STATE: { @@ -262,6 +271,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) { break; } case '@@redux/INIT': { + replaying = false; + // Always recompute states on hot reload and init. minInvalidatedStateIndex = 0; break; @@ -281,7 +292,8 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) { committedState, actionsById, stagedActionIds, - skippedActionIds + skippedActionIds, + replaying ); monitorState = monitorReducer(monitorState, liftedAction); return {