Skip to content

Commit

Permalink
feat(StoreDevtools): Add support for jumping to a specific action (#703)
Browse files Browse the repository at this point in the history
Closes #681
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Jan 7, 2018
1 parent 8e93328 commit b9f6442
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/store-devtools/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ describe('Store Devtools', () => {
expect(getState()).toBe(2);
});

it('should jump to action', () => {
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'DECREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(getState()).toBe(1);

devtools.jumpToAction(2);
expect(getState()).toBe(0);
});

it('should replace the reducer and preserve previous states', () => {
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'DECREMENT' });
Expand Down
8 changes: 8 additions & 0 deletions modules/store-devtools/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const SWEEP = 'SWEEP';
export const TOGGLE_ACTION = 'TOGGLE_ACTION';
export const SET_ACTIONS_ACTIVE = 'SET_ACTIONS_ACTIVE';
export const JUMP_TO_STATE = 'JUMP_TO_STATE';
export const JUMP_TO_ACTION = 'JUMP_TO_ACTION';
export const IMPORT_STATE = 'IMPORT_STATE';

export class PerformAction implements Action {
Expand Down Expand Up @@ -67,6 +68,12 @@ export class JumpToState implements Action {
constructor(public index: number) {}
}

export class JumpToAction implements Action {
readonly type = JUMP_TO_ACTION;

constructor(public actionId: number) {}
}

export class ImportState implements Action {
readonly type = IMPORT_STATE;

Expand All @@ -82,4 +89,5 @@ export type All =
| ToggleAction
| SetActionsActive
| JumpToState
| JumpToAction
| ImportState;
4 changes: 4 additions & 0 deletions modules/store-devtools/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export class StoreDevtools implements Observer<any> {
this.dispatch(new Actions.ToggleAction(id));
}

jumpToAction(actionId: number) {
this.dispatch(new Actions.JumpToAction(actionId));
}

jumpToState(index: number) {
this.dispatch(new Actions.JumpToState(index));
}
Expand Down
8 changes: 8 additions & 0 deletions modules/store-devtools/src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ export function liftReducerWith(
minInvalidatedStateIndex = Infinity;
break;
}
case Actions.JUMP_TO_ACTION: {
// Jumps to a corresponding state to a specific action.
// Useful when filtering actions.
const index = stagedActionIds.indexOf(liftedAction.actionId);
if (index !== -1) currentStateIndex = index;
minInvalidatedStateIndex = Infinity;
break;
}
case Actions.SWEEP: {
// Forget any actions that are currently being skipped.
stagedActionIds = difference(stagedActionIds, skippedActionIds);
Expand Down

0 comments on commit b9f6442

Please sign in to comment.