Skip to content

Commit 00be3d1

Browse files
rupeshtiwaribrandonroberts
authored andcommitted
feat(StoreDevtools): Add support for custom instance name (#517)
Closes #463
1 parent aedf20e commit 00be3d1

File tree

9 files changed

+115
-21
lines changed

9 files changed

+115
-21
lines changed

modules/router-store/spec/integration.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('integration spec', () => {
4444
{ type: 'router', event: 'NavigationStart', url: '/' },
4545
{ type: 'router', event: 'RoutesRecognized', url: '/' },
4646
{ type: 'store', state: '/' }, // ROUTER_NAVIGATION event in the store
47-
4847
/* new Router Lifecycle in Angular 4.3 */
4948
{ type: 'router', event: 'GuardsCheckStart', url: '/' },
5049
{ type: 'router', event: 'GuardsCheckEnd', url: '/' },
@@ -162,7 +161,6 @@ describe('integration spec', () => {
162161
{ type: 'router', event: 'GuardsCheckEnd', url: '/next' },
163162
// { type: 'router', event: 'ResolveStart', url: '/next' },
164163
// { type: 'router', event: 'ResolveEnd', url: '/next' },
165-
166164
{
167165
type: 'store',
168166
state: {
@@ -451,7 +449,6 @@ describe('integration spec', () => {
451449
{ type: 'router', event: 'NavigationStart', url: '/next' },
452450
{ type: 'router', event: 'RoutesRecognized', url: '/next' },
453451
{ type: 'store', state: undefined }, // after ROUTER_NAVIGATION
454-
455452
/* new Router Lifecycle in Angular 4.3 */
456453
{ type: 'router', event: 'GuardsCheckStart', url: '/next' },
457454
{ type: 'store', state: undefined }, // after USER_EVENT

modules/router-store/src/router_store_module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ export class StoreRouterConnectingModule {
221221

222222
private dispatchTriggeredByRouter: boolean = false; // used only in dev mode in combination with routerReducer
223223
private navigationTriggeredByDispatch: boolean = false; // used only in dev mode in combination with routerReducer
224-
225224
private stateKey: string;
226225

227226
constructor(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ActionReducer, Action } from '@ngrx/store';
2+
import { StoreDevtoolsConfig } from '../';
3+
4+
describe('StoreDevtoolsOptions', () => {
5+
it('can be initialized with name', () => {
6+
const options = new StoreDevtoolsConfig();
7+
options.name = 'my instance';
8+
expect(options.name).toBe('my instance');
9+
});
10+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
StoreDevtools,
3+
StoreDevtoolsModule,
4+
LiftedState,
5+
StoreDevtoolsConfig,
6+
StoreDevtoolsOptions,
7+
} from '../';
8+
import {
9+
StoreModule,
10+
Store,
11+
StateObservable,
12+
ActionReducer,
13+
Action,
14+
ReducerManager,
15+
} from '@ngrx/store';
16+
import { of } from 'rxjs/observable/of';
17+
import { createConfig, noMonitor } from '../src/instrument';
18+
import { DevtoolsExtension, ReduxDevtoolsExtension } from '../src/extension';
19+
20+
describe('DevtoolsExtension', () => {
21+
let reduxDevtoolsExtension: ReduxDevtoolsExtension;
22+
let devtoolsExtension: DevtoolsExtension;
23+
24+
beforeEach(() => {
25+
reduxDevtoolsExtension = jasmine.createSpyObj('reduxDevtoolsExtension', [
26+
'send',
27+
'connect',
28+
]);
29+
(reduxDevtoolsExtension.connect as jasmine.Spy).and.returnValue(of({}));
30+
spyOn(Date, 'now').and.returnValue('1509655064369');
31+
});
32+
33+
describe('notify', () => {
34+
it('should send notification with default options', () => {
35+
devtoolsExtension = new DevtoolsExtension(
36+
reduxDevtoolsExtension,
37+
createConfig({})
38+
);
39+
const defaultOptions = {
40+
maxAge: false,
41+
monitor: noMonitor,
42+
name: 'NgRx Store DevTools',
43+
serialize: false,
44+
};
45+
const action = {} as Action;
46+
const state = {} as LiftedState;
47+
devtoolsExtension.notify(action, state);
48+
expect(reduxDevtoolsExtension.send).toHaveBeenCalledWith(
49+
null,
50+
{},
51+
defaultOptions,
52+
'ngrx-store-1509655064369'
53+
);
54+
});
55+
it('should send notification with given options', () => {
56+
devtoolsExtension = new DevtoolsExtension(
57+
reduxDevtoolsExtension,
58+
createConfig({
59+
name: 'ngrx-store-devtool-todolist',
60+
})
61+
);
62+
const defaultOptions = {
63+
maxAge: false,
64+
monitor: noMonitor,
65+
name: 'ngrx-store-devtool-todolist',
66+
serialize: false,
67+
};
68+
const action = {} as Action;
69+
const state = {} as LiftedState;
70+
devtoolsExtension.notify(action, state);
71+
expect(reduxDevtoolsExtension.send).toHaveBeenCalledWith(
72+
null,
73+
{},
74+
defaultOptions,
75+
'ngrx-store-1509655064369'
76+
);
77+
});
78+
});
79+
});

modules/store-devtools/src/config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { ActionReducer } from '@ngrx/store';
1+
import { ActionReducer, Action } from '@ngrx/store';
22
import { InjectionToken, Type } from '@angular/core';
33

44
export class StoreDevtoolsConfig {
5-
maxAge: number | false;
6-
monitor: ActionReducer<any, any>;
5+
maxAge?: number | false;
6+
monitor?: ActionReducer<any, any>;
7+
name?: string;
8+
serialize?: boolean;
79
}
810

911
export const STORE_DEVTOOLS_CONFIG = new InjectionToken<StoreDevtoolsConfig>(

modules/store-devtools/src/devtools.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export class StoreDevtools implements Observer<any> {
4949
initialState,
5050
liftedInitialState,
5151
config.monitor,
52-
config.maxAge ? { maxAge: config.maxAge } : {}
52+
{
53+
maxAge: config.maxAge as number,
54+
name: config.name,
55+
serialize: config.serialize,
56+
}
5357
);
5458

5559
const liftedAction$ = applyOperators(actions$.asObservable(), [
@@ -80,9 +84,9 @@ export class StoreDevtools implements Observer<any> {
8084
liftedStateSubject.next(state);
8185

8286
if (action.type === Actions.PERFORM_ACTION) {
83-
const unlifedAction = (action as Actions.PerformAction).action;
87+
const unliftedAction = (action as Actions.PerformAction).action;
8488

85-
scannedActions.next(unlifedAction);
89+
scannedActions.next(unliftedAction);
8690
}
8791
});
8892

modules/store-devtools/src/extension.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { InjectionToken, Inject, Injectable } from '@angular/core';
1+
import { Inject, Injectable, InjectionToken } from '@angular/core';
2+
import { Action } from '@ngrx/store';
23
import { Observable } from 'rxjs/Observable';
34
import { empty } from 'rxjs/observable/empty';
45
import { filter } from 'rxjs/operator/filter';
56
import { map } from 'rxjs/operator/map';
67
import { share } from 'rxjs/operator/share';
78
import { switchMap } from 'rxjs/operator/switchMap';
89
import { takeUntil } from 'rxjs/operator/takeUntil';
9-
import { Action } from '@ngrx/store';
10+
11+
import { STORE_DEVTOOLS_CONFIG, StoreDevtoolsConfig } from './config';
1012
import { LiftedState } from './reducer';
1113
import { applyOperators } from './utils';
1214

@@ -35,7 +37,7 @@ export interface ReduxDevtoolsExtension {
3537
send(
3638
action: any,
3739
state: any,
38-
options?: boolean | { serialize: boolean | object },
40+
options: StoreDevtoolsConfig,
3941
instanceId?: string
4042
): void;
4143
}
@@ -49,7 +51,8 @@ export class DevtoolsExtension {
4951
actions$: Observable<any>;
5052

5153
constructor(
52-
@Inject(REDUX_DEVTOOLS_EXTENSION) devtoolsExtension: ReduxDevtoolsExtension
54+
@Inject(REDUX_DEVTOOLS_EXTENSION) devtoolsExtension: ReduxDevtoolsExtension,
55+
@Inject(STORE_DEVTOOLS_CONFIG) private config: StoreDevtoolsConfig
5356
) {
5457
this.devtoolsExtension = devtoolsExtension;
5558
this.createActionStreams();
@@ -60,12 +63,7 @@ export class DevtoolsExtension {
6063
return;
6164
}
6265

63-
this.devtoolsExtension.send(
64-
null,
65-
state,
66-
{ serialize: false },
67-
this.instanceId
68-
);
66+
this.devtoolsExtension.send(null, state, this.config, this.instanceId);
6967
}
7068

7169
private createChangesObservable(): Observable<any> {

modules/store-devtools/src/instrument.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ export function noMonitor(): null {
6464
return null;
6565
}
6666

67+
export const DEFAULT_NAME = 'NgRx Store DevTools';
68+
6769
export function createConfig(
6870
_options: StoreDevtoolsOptions
6971
): StoreDevtoolsConfig {
7072
const DEFAULT_OPTIONS: StoreDevtoolsConfig = {
7173
maxAge: false,
7274
monitor: noMonitor,
75+
name: DEFAULT_NAME,
76+
serialize: false,
7377
};
7478

7579
let options = typeof _options === 'function' ? _options() : _options;

modules/store-devtools/src/reducer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import { difference, liftAction } from './utils';
1111
import * as Actions from './actions';
12+
import { StoreDevtoolsConfig } from './config';
1213

1314
export type InitAction = {
1415
readonly type: typeof INIT;
@@ -129,7 +130,7 @@ export function liftReducerWith(
129130
initialCommittedState: any,
130131
initialLiftedState: LiftedState,
131132
monitorReducer?: any,
132-
options: { maxAge?: number } = {}
133+
options: Partial<StoreDevtoolsConfig> = {}
133134
) {
134135
/**
135136
* Manages how the history actions modify the history state.

0 commit comments

Comments
 (0)