Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(StoreDevtools): Add support for jumping to a specific action #703

Merged
merged 1 commit into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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