Skip to content

Commit 72b59c3

Browse files
committed
Merge pull request #248 from khankuan/master
Consecutive toggle
2 parents b7f0b26 + cbc1d0e commit 72b59c3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/instrument.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import difference from 'lodash/difference';
2+
import union from 'lodash/union';
23

34
export const ActionTypes = {
45
PERFORM_ACTION: 'PERFORM_ACTION',
@@ -7,6 +8,7 @@ export const ActionTypes = {
78
COMMIT: 'COMMIT',
89
SWEEP: 'SWEEP',
910
TOGGLE_ACTION: 'TOGGLE_ACTION',
11+
SET_ACTIONS_ACTIVE: 'SET_ACTIONS_ACTIVE',
1012
JUMP_TO_STATE: 'JUMP_TO_STATE',
1113
IMPORT_STATE: 'IMPORT_STATE'
1214
};
@@ -45,6 +47,10 @@ export const ActionCreators = {
4547
return { type: ActionTypes.TOGGLE_ACTION, id };
4648
},
4749

50+
setActionsActive(start, end, active=true) {
51+
return { type: ActionTypes.SET_ACTIONS_ACTIVE, start, end, active };
52+
},
53+
4854
jumpToState(index) {
4955
return { type: ActionTypes.JUMP_TO_STATE, index };
5056
},
@@ -244,6 +250,22 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer, options
244250
minInvalidatedStateIndex = stagedActionIds.indexOf(actionId);
245251
break;
246252
}
253+
case ActionTypes.SET_ACTIONS_ACTIVE: {
254+
// Toggle whether an action with given ID is skipped.
255+
// Being skipped means it is a no-op during the computation.
256+
const { start, end, active } = liftedAction;
257+
const actionIds = [];
258+
for (let i = start; i < end; i++) actionIds.push(i);
259+
if (active) {
260+
skippedActionIds = difference(skippedActionIds, actionIds);
261+
} else {
262+
skippedActionIds = union(skippedActionIds, actionIds);
263+
}
264+
265+
// Optimization: we know history before this action hasn't changed
266+
minInvalidatedStateIndex = stagedActionIds.indexOf(start);
267+
break;
268+
}
247269
case ActionTypes.JUMP_TO_STATE: {
248270
// Without recomputing anything, move the pointer that tell us
249271
// which state is considered the current one. Useful for sliders.

test/instrument.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ describe('instrument', () => {
109109
expect(store.getState()).toBe(1);
110110
});
111111

112+
it('should set multiple action skip', () => {
113+
// actionId 0 = @@INIT
114+
store.dispatch({ type: 'INCREMENT' });
115+
store.dispatch({ type: 'INCREMENT' });
116+
store.dispatch({ type: 'INCREMENT' });
117+
expect(store.getState()).toBe(3);
118+
119+
liftedStore.dispatch(ActionCreators.setActionsActive(1, 3, false));
120+
expect(store.getState()).toBe(1);
121+
122+
liftedStore.dispatch(ActionCreators.setActionsActive(0, 2, true));
123+
expect(store.getState()).toBe(2);
124+
125+
liftedStore.dispatch(ActionCreators.setActionsActive(0, 1, true));
126+
expect(store.getState()).toBe(2);
127+
});
128+
112129
it('should sweep disabled actions', () => {
113130
// actionId 0 = @@INIT
114131
store.dispatch({ type: 'INCREMENT' });

0 commit comments

Comments
 (0)