Skip to content

Commit c6e33d9

Browse files
nskandranibrandonroberts
authored andcommitted
fix(StoreDevtools): Refresh devtools when extension is started (#1017)
Closes #508
1 parent df6e78c commit c6e33d9

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ describe('Store Devtools', () => {
177177
expect(getState()).toBe(2);
178178
});
179179

180+
it('should refresh to show current state as is', () => {
181+
// actionId 0 = @@INIT
182+
store.dispatch({ type: 'INCREMENT' });
183+
store.dispatch({ type: 'INCREMENT' });
184+
store.dispatch({ type: 'INCREMENT' });
185+
store.dispatch({ type: 'INCREMENT' });
186+
187+
expect(getState()).toBe(4);
188+
expect(getLiftedState().stagedActionIds).toEqual([0, 1, 2, 3, 4]);
189+
expect(getLiftedState().skippedActionIds).toEqual([]);
190+
191+
devtools.refresh();
192+
193+
expect(getState()).toBe(4);
194+
expect(getLiftedState().stagedActionIds).toEqual([0, 1, 2, 3, 4]);
195+
expect(getLiftedState().skippedActionIds).toEqual([]);
196+
});
197+
180198
it('should reset to initial state', () => {
181199
store.dispatch({ type: 'INCREMENT' });
182200
expect(getState()).toBe(1);

modules/store-devtools/src/actions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Action } from '@ngrx/store';
22

33
export const PERFORM_ACTION = 'PERFORM_ACTION';
4+
export const REFRESH = 'REFRESH';
45
export const RESET = 'RESET';
56
export const ROLLBACK = 'ROLLBACK';
67
export const COMMIT = 'COMMIT';
@@ -24,6 +25,10 @@ export class PerformAction implements Action {
2425
}
2526
}
2627

28+
export class Refresh implements Action {
29+
readonly type = REFRESH;
30+
}
31+
2732
export class Reset implements Action {
2833
readonly type = RESET;
2934

@@ -82,6 +87,7 @@ export class ImportState implements Action {
8287

8388
export type All =
8489
| PerformAction
90+
| Refresh
8591
| Reset
8692
| Rollback
8793
| Commit

modules/store-devtools/src/devtools.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class DevtoolsDispatcher extends ActionsSubject {}
2929
@Injectable()
3030
export class StoreDevtools implements Observer<any> {
3131
private stateSubscription: Subscription;
32+
private extensionStartSubscription: Subscription;
3233
public dispatcher: ActionsSubject;
3334
public liftedState: Observable<LiftedState>;
3435
public state: Observable<any>;
@@ -95,11 +96,16 @@ export class StoreDevtools implements Observer<any> {
9596
}
9697
});
9798

99+
const extensionStartSubscription = extension.start$.subscribe(() => {
100+
this.refresh();
101+
});
102+
98103
const liftedState$ = liftedStateSubject.asObservable() as Observable<
99104
LiftedState
100105
>;
101106
const state$ = liftedState$.pipe(map(unliftState));
102107

108+
this.extensionStartSubscription = extensionStartSubscription;
103109
this.stateSubscription = liftedStateSubscription;
104110
this.dispatcher = dispatcher;
105111
this.liftedState = liftedState$;
@@ -122,6 +128,10 @@ export class StoreDevtools implements Observer<any> {
122128
this.dispatch(new Actions.PerformAction(action, +Date.now()));
123129
}
124130

131+
refresh() {
132+
this.dispatch(new Actions.Refresh());
133+
}
134+
125135
reset() {
126136
this.dispatch(new Actions.Reset(+Date.now()));
127137
}

modules/store-devtools/src/extension.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class DevtoolsExtension {
6060

6161
liftedActions$: Observable<any>;
6262
actions$: Observable<any>;
63+
start$: Observable<any>;
6364

6465
constructor(
6566
@Inject(REDUX_DEVTOOLS_EXTENSION) devtoolsExtension: ReduxDevtoolsExtension,
@@ -169,10 +170,11 @@ export class DevtoolsExtension {
169170

170171
const actionsUntilStop$ = actions$.pipe(takeUntil(stop$));
171172
const liftedUntilStop$ = liftedActions$.pipe(takeUntil(stop$));
173+
this.start$ = start$.pipe(takeUntil(stop$));
172174

173175
// Only take the action sources between the start/stop events
174-
this.actions$ = start$.pipe(switchMap(() => actionsUntilStop$));
175-
this.liftedActions$ = start$.pipe(switchMap(() => liftedUntilStop$));
176+
this.actions$ = this.start$.pipe(switchMap(() => actionsUntilStop$));
177+
this.liftedActions$ = this.start$.pipe(switchMap(() => liftedUntilStop$));
176178
}
177179

178180
private unwrapAction(action: Action) {

0 commit comments

Comments
 (0)