Skip to content

Commit cbc1d0e

Browse files
committedFeb 13, 2016
Added SET_ACTIONS_ACTIVE, ability to set multiple actions between start and end indices to be active or skipped
1 parent a87564c commit cbc1d0e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎src/instrument.js

Lines changed: 22 additions & 0 deletions
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
},
@@ -219,6 +225,22 @@ function liftReducerWith(reducer, initialCommittedState, monitorReducer) {
219225
minInvalidatedStateIndex = stagedActionIds.indexOf(actionId);
220226
break;
221227
}
228+
case ActionTypes.SET_ACTIONS_ACTIVE: {
229+
// Toggle whether an action with given ID is skipped.
230+
// Being skipped means it is a no-op during the computation.
231+
const { start, end, active } = liftedAction;
232+
const actionIds = [];
233+
for (let i = start; i < end; i++) actionIds.push(i);
234+
if (active) {
235+
skippedActionIds = difference(skippedActionIds, actionIds);
236+
} else {
237+
skippedActionIds = union(skippedActionIds, actionIds);
238+
}
239+
240+
// Optimization: we know history before this action hasn't changed
241+
minInvalidatedStateIndex = stagedActionIds.indexOf(start);
242+
break;
243+
}
222244
case ActionTypes.JUMP_TO_STATE: {
223245
// Without recomputing anything, move the pointer that tell us
224246
// which state is considered the current one. Useful for sliders.

‎test/instrument.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ describe('instrument', () => {
100100
expect(store.getState()).toBe(1);
101101
});
102102

103+
it('should set multiple action skip', () => {
104+
// actionId 0 = @@INIT
105+
store.dispatch({ type: 'INCREMENT' });
106+
store.dispatch({ type: 'INCREMENT' });
107+
store.dispatch({ type: 'INCREMENT' });
108+
expect(store.getState()).toBe(3);
109+
110+
liftedStore.dispatch(ActionCreators.setActionsActive(1, 3, false));
111+
expect(store.getState()).toBe(1);
112+
113+
liftedStore.dispatch(ActionCreators.setActionsActive(0, 2, true));
114+
expect(store.getState()).toBe(2);
115+
116+
liftedStore.dispatch(ActionCreators.setActionsActive(0, 1, true));
117+
expect(store.getState()).toBe(2);
118+
});
119+
103120
it('should sweep disabled actions', () => {
104121
// actionId 0 = @@INIT
105122
store.dispatch({ type: 'INCREMENT' });

0 commit comments

Comments
 (0)