Skip to content

Commit

Permalink
feat(StoreDevtools): Add support for extension sanitizers (#544)
Browse files Browse the repository at this point in the history
Closes #494
  • Loading branch information
rupeshtiwari authored and brandonroberts committed Nov 16, 2017
1 parent 41723fc commit 6ed92b0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 24 deletions.
18 changes: 18 additions & 0 deletions modules/store-devtools/spec/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,22 @@ describe('StoreDevtoolsOptions', () => {
options.name = 'my instance';
expect(options.name).toBe('my instance');
});

it('can be initialized with actionSanitizer', () => {
const options = new StoreDevtoolsConfig();
function sanitizer(action: Action, id: number): Action {
return action;
}
options.actionSanitizer = sanitizer;
expect(options.actionSanitizer).toEqual(sanitizer);
});

it('can be initialized with stateSanitizer', () => {
const options = new StoreDevtoolsConfig();
function stateSanitizer(state: any, index: number): any {
return state;
}
options.stateSanitizer = stateSanitizer;
expect(options.stateSanitizer).toEqual(stateSanitizer);
});
});
39 changes: 23 additions & 16 deletions modules/store-devtools/spec/extension.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import {
StoreDevtools,
StoreDevtoolsModule,
LiftedState,
StoreDevtoolsConfig,
StoreDevtoolsOptions,
} from '../';
import {
StoreModule,
Store,
StateObservable,
ActionReducer,
Action,
ReducerManager,
} from '@ngrx/store';
import { Action } from '@ngrx/store';
import { of } from 'rxjs/observable/of';
import { createConfig, noMonitor } from '../src/instrument';

import { LiftedState } from '../';
import { DevtoolsExtension, ReduxDevtoolsExtension } from '../src/extension';
import {
createConfig,
noActionSanitizer,
noMonitor,
noStateSanitizer,
} from '../src/instrument';

describe('DevtoolsExtension', () => {
let reduxDevtoolsExtension: ReduxDevtoolsExtension;
Expand All @@ -39,6 +32,8 @@ describe('DevtoolsExtension', () => {
const defaultOptions = {
maxAge: false,
monitor: noMonitor,
actionSanitizer: noActionSanitizer,
stateSanitizer: noStateSanitizer,
name: 'NgRx Store DevTools',
serialize: false,
};
Expand All @@ -52,16 +47,28 @@ describe('DevtoolsExtension', () => {
'ngrx-store-1509655064369'
);
});

function myActionSanitizer() {
return { type: 'sanitizer' };
}
function myStateSanitizer() {
return { state: 'new state' };
}

it('should send notification with given options', () => {
devtoolsExtension = new DevtoolsExtension(
reduxDevtoolsExtension,
createConfig({
actionSanitizer: myActionSanitizer,
stateSanitizer: myStateSanitizer,
name: 'ngrx-store-devtool-todolist',
})
);
const defaultOptions = {
maxAge: false,
monitor: noMonitor,
actionSanitizer: myActionSanitizer,
stateSanitizer: myStateSanitizer,
name: 'ngrx-store-devtool-todolist',
serialize: false,
};
Expand Down
6 changes: 4 additions & 2 deletions modules/store-devtools/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { ActionReducer, Action } from '@ngrx/store';
import { InjectionToken, Type } from '@angular/core';

export class StoreDevtoolsConfig {
maxAge?: number | false;
monitor?: ActionReducer<any, any>;
maxAge: number | false;
monitor: ActionReducer<any, any>;
actionSanitizer?: <A extends Action>(action: A, id: number) => A;
stateSanitizer?: <S>(state: S, index: number) => S;
name?: string;
serialize?: boolean;
}
Expand Down
6 changes: 1 addition & 5 deletions modules/store-devtools/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ export class StoreDevtools implements Observer<any> {
initialState,
liftedInitialState,
config.monitor,
{
maxAge: config.maxAge as number,
name: config.name,
serialize: config.serialize,
}
config
);

const liftedAction$ = applyOperators(actions$.asObservable(), [
Expand Down
10 changes: 10 additions & 0 deletions modules/store-devtools/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export function noMonitor(): null {
return null;
}

export function noActionSanitizer(): null {
return null;
}

export function noStateSanitizer(): null {
return null;
}

export const DEFAULT_NAME = 'NgRx Store DevTools';

export function createConfig(
Expand All @@ -72,6 +80,8 @@ export function createConfig(
const DEFAULT_OPTIONS: StoreDevtoolsConfig = {
maxAge: false,
monitor: noMonitor,
actionSanitizer: noActionSanitizer,
stateSanitizer: noStateSanitizer,
name: DEFAULT_NAME,
serialize: false,
};
Expand Down
1 change: 0 additions & 1 deletion modules/store-devtools/src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
UPDATE,
INIT,
} from '@ngrx/store';

import { difference, liftAction } from './utils';
import * as Actions from './actions';
import { StoreDevtoolsConfig } from './config';
Expand Down

0 comments on commit 6ed92b0

Please sign in to comment.