Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not recompute states unless necessary #133

Merged
merged 2 commits into from
Sep 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/devTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function liftReducer(reducer, initialState) {
* Manages how the DevTools actions modify the DevTools state.
*/
return function liftedReducer(liftedState = initialLiftedState, liftedAction) {
let shouldRecomputeStates = true;
let {
committedState,
stagedActions,
Expand Down Expand Up @@ -131,6 +132,8 @@ function liftReducer(reducer, initialState) {
break;
case ActionTypes.JUMP_TO_STATE:
currentStateIndex = liftedAction.index;
// Optimization: we know the history has not changed.
shouldRecomputeStates = false;
break;
case ActionTypes.SWEEP:
stagedActions = stagedActions.filter((_, i) => !skippedActions[i]);
Expand All @@ -142,8 +145,21 @@ function liftReducer(reducer, initialState) {
if (currentStateIndex === stagedActions.length - 1) {
currentStateIndex++;
}

stagedActions = [...stagedActions, liftedAction.action];
timestamps = [...timestamps, liftedAction.timestamp];

// Optimization: we know that the past has not changed.
shouldRecomputeStates = false;
// Instead of recomputing the states, append the next one.
const previousEntry = computedStates[computedStates.length - 1];
const nextEntry = computeNextEntry(
reducer,
liftedAction.action,
previousEntry.state,
previousEntry.error
);
computedStates = [...computedStates, nextEntry];
break;
case ActionTypes.SET_MONITOR_STATE:
monitorState = liftedAction.monitorState;
Expand All @@ -159,12 +175,14 @@ function liftReducer(reducer, initialState) {
break;
}

computedStates = recomputeStates(
reducer,
committedState,
stagedActions,
skippedActions
);
if (shouldRecomputeStates) {
computedStates = recomputeStates(
reducer,
committedState,
stagedActions,
skippedActions
);
}

return {
committedState,
Expand Down
31 changes: 31 additions & 0 deletions test/devTools.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,35 @@ describe('devTools', () => {
storeWithBug.dispatch({ type: 'SET_UNDEFINED' });
expect(storeWithBug.getState()).toBe(2);
});

it('should not recompute states on every action', () => {
let reducerCalls = 0;
let monitoredStore = devTools()(createStore)(() => reducerCalls++);
expect(reducerCalls).toBe(1);
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
expect(reducerCalls).toBe(4);
});

it('should not recompute states when jumping to state', () => {
let reducerCalls = 0;
let monitoredStore = devTools()(createStore)(() => reducerCalls++);
let monitoredDevToolsStore = monitoredStore.devToolsStore;

expect(reducerCalls).toBe(1);
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
expect(reducerCalls).toBe(4);

monitoredDevToolsStore.dispatch(ActionCreators.jumpToState(0));
expect(reducerCalls).toBe(4);

monitoredDevToolsStore.dispatch(ActionCreators.jumpToState(1));
expect(reducerCalls).toBe(4);

monitoredDevToolsStore.dispatch(ActionCreators.jumpToState(3));
expect(reducerCalls).toBe(4);
});
});