Skip to content

Commit b9f6442

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
feat(StoreDevtools): Add support for jumping to a specific action (#703)
Closes #681
1 parent 8e93328 commit b9f6442

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

modules/store-devtools/spec/store.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ describe('Store Devtools', () => {
259259
expect(getState()).toBe(2);
260260
});
261261

262+
it('should jump to action', () => {
263+
store.dispatch({ type: 'INCREMENT' });
264+
store.dispatch({ type: 'DECREMENT' });
265+
store.dispatch({ type: 'INCREMENT' });
266+
expect(getState()).toBe(1);
267+
268+
devtools.jumpToAction(2);
269+
expect(getState()).toBe(0);
270+
});
271+
262272
it('should replace the reducer and preserve previous states', () => {
263273
store.dispatch({ type: 'INCREMENT' });
264274
store.dispatch({ type: 'DECREMENT' });

modules/store-devtools/src/actions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const SWEEP = 'SWEEP';
88
export const TOGGLE_ACTION = 'TOGGLE_ACTION';
99
export const SET_ACTIONS_ACTIVE = 'SET_ACTIONS_ACTIVE';
1010
export const JUMP_TO_STATE = 'JUMP_TO_STATE';
11+
export const JUMP_TO_ACTION = 'JUMP_TO_ACTION';
1112
export const IMPORT_STATE = 'IMPORT_STATE';
1213

1314
export class PerformAction implements Action {
@@ -67,6 +68,12 @@ export class JumpToState implements Action {
6768
constructor(public index: number) {}
6869
}
6970

71+
export class JumpToAction implements Action {
72+
readonly type = JUMP_TO_ACTION;
73+
74+
constructor(public actionId: number) {}
75+
}
76+
7077
export class ImportState implements Action {
7178
readonly type = IMPORT_STATE;
7279

@@ -82,4 +89,5 @@ export type All =
8289
| ToggleAction
8390
| SetActionsActive
8491
| JumpToState
92+
| JumpToAction
8593
| ImportState;

modules/store-devtools/src/devtools.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export class StoreDevtools implements Observer<any> {
133133
this.dispatch(new Actions.ToggleAction(id));
134134
}
135135

136+
jumpToAction(actionId: number) {
137+
this.dispatch(new Actions.JumpToAction(actionId));
138+
}
139+
136140
jumpToState(index: number) {
137141
this.dispatch(new Actions.JumpToState(index));
138142
}

modules/store-devtools/src/reducer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ export function liftReducerWith(
259259
minInvalidatedStateIndex = Infinity;
260260
break;
261261
}
262+
case Actions.JUMP_TO_ACTION: {
263+
// Jumps to a corresponding state to a specific action.
264+
// Useful when filtering actions.
265+
const index = stagedActionIds.indexOf(liftedAction.actionId);
266+
if (index !== -1) currentStateIndex = index;
267+
minInvalidatedStateIndex = Infinity;
268+
break;
269+
}
262270
case Actions.SWEEP: {
263271
// Forget any actions that are currently being skipped.
264272
stagedActionIds = difference(stagedActionIds, skippedActionIds);

0 commit comments

Comments
 (0)