Skip to content
This repository was archived by the owner on Dec 21, 2018. It is now read-only.

Commit 2b46c56

Browse files
authored
Merge pull request #8 from zalmoxisus/lock
Lock changes
2 parents bba076d + b9540b3 commit 2b46c56

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/instrument.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const ActionTypes = {
1212
TOGGLE_ACTION: 'TOGGLE_ACTION',
1313
SET_ACTIONS_ACTIVE: 'SET_ACTIONS_ACTIVE',
1414
JUMP_TO_STATE: 'JUMP_TO_STATE',
15-
IMPORT_STATE: 'IMPORT_STATE'
15+
IMPORT_STATE: 'IMPORT_STATE',
16+
LOCK_CHANGES: 'LOCK_CHANGES'
1617
};
1718

1819
/**
@@ -67,6 +68,10 @@ export const ActionCreators = {
6768

6869
importState(nextLiftedState, noRecompute) {
6970
return { type: ActionTypes.IMPORT_STATE, nextLiftedState, noRecompute };
71+
},
72+
73+
lockChanges(status) {
74+
return { type: ActionTypes.LOCK_CHANGES, status };
7075
}
7176
};
7277

@@ -175,7 +180,8 @@ export function liftReducerWith(reducer, initialCommittedState, monitorReducer,
175180
skippedActionIds: [],
176181
committedState: initialCommittedState,
177182
currentStateIndex: 0,
178-
computedStates: []
183+
computedStates: [],
184+
dropNewActions: false
179185
};
180186

181187
/**
@@ -190,7 +196,8 @@ export function liftReducerWith(reducer, initialCommittedState, monitorReducer,
190196
skippedActionIds,
191197
committedState,
192198
currentStateIndex,
193-
computedStates
199+
computedStates,
200+
dropNewActions
194201
} = liftedState || initialLiftedState;
195202

196203
if (!liftedState) {
@@ -309,6 +316,10 @@ export function liftReducerWith(reducer, initialCommittedState, monitorReducer,
309316
break;
310317
}
311318
case ActionTypes.PERFORM_ACTION: {
319+
if (dropNewActions) {
320+
return liftedState || initialLiftedState;
321+
}
322+
312323
// Auto-commit as new actions come in.
313324
if (options.maxAge && stagedActionIds.length === options.maxAge) {
314325
commitExcessActions(1);
@@ -363,6 +374,11 @@ export function liftReducerWith(reducer, initialCommittedState, monitorReducer,
363374

364375
break;
365376
}
377+
case ActionTypes.LOCK_CHANGES: {
378+
dropNewActions = liftedAction.status;
379+
minInvalidatedStateIndex = Infinity;
380+
break;
381+
}
366382
case '@@redux/INIT': {
367383
// Always recompute states on hot reload and init.
368384
minInvalidatedStateIndex = 0;
@@ -415,7 +431,8 @@ export function liftReducerWith(reducer, initialCommittedState, monitorReducer,
415431
skippedActionIds,
416432
committedState,
417433
currentStateIndex,
418-
computedStates
434+
computedStates,
435+
dropNewActions
419436
};
420437
};
421438
}

test/instrument.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,33 @@ describe('instrument', () => {
635635
});
636636
});
637637

638+
describe('Lock Changes', () => {
639+
it('should lock', () => {
640+
store.dispatch({ type: 'INCREMENT' });
641+
store.liftedStore.dispatch({ type: 'LOCK_CHANGES', status: true });
642+
expect(store.liftedStore.getState().dropNewActions).toBe(true);
643+
expect(store.liftedStore.getState().nextActionId).toBe(2);
644+
expect(store.getState()).toBe(1);
645+
646+
store.dispatch({ type: 'INCREMENT' });
647+
expect(store.liftedStore.getState().nextActionId).toBe(2);
648+
expect(store.getState()).toBe(1);
649+
650+
liftedStore.dispatch(ActionCreators.toggleAction(1));
651+
expect(store.getState()).toBe(0);
652+
liftedStore.dispatch(ActionCreators.toggleAction(1));
653+
expect(store.getState()).toBe(1);
654+
655+
store.liftedStore.dispatch({ type: 'LOCK_CHANGES', status: false });
656+
expect(store.liftedStore.getState().dropNewActions).toBe(false);
657+
expect(store.liftedStore.getState().nextActionId).toBe(2);
658+
659+
store.dispatch({ type: 'INCREMENT' });
660+
expect(store.liftedStore.getState().nextActionId).toBe(3);
661+
expect(store.getState()).toBe(2);
662+
});
663+
});
664+
638665
it('throws if reducer is not a function', () => {
639666
expect(() =>
640667
createStore(undefined, instrument())

0 commit comments

Comments
 (0)