Skip to content

Commit

Permalink
revert: warn when same action is registered (#1801) (#1841)
Browse files Browse the repository at this point in the history
This reverts commit ecda5f7
  • Loading branch information
timdeschryver authored and brandonroberts committed May 9, 2019
1 parent eec9cc6 commit b07ae4e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 62 deletions.
54 changes: 0 additions & 54 deletions modules/store/spec/reducer_creator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as ngCore from '@angular/core';
import { on, createReducer, createAction, props, union } from '@ngrx/store';
import { expecter } from 'ts-snippet';

Expand Down Expand Up @@ -87,59 +86,6 @@ import {on} from './modules/store/src/reducer_creator';
state = fooBarReducer(state, bar({ bar: 54 }));
expect(state).toEqual(['[foobar] FOO', '[foobar] BAR']);
});

describe('warning message when the same action type gets registered', () => {
it('should not warn when not violated', () => {
const spy = spyOn(console, 'warn');

const fooBarReducer = createReducer(
[],
on(foo, state => state),
on(bar, state => state)
);

expect(spy).not.toHaveBeenCalled();
});

it('should warn in dev mode', () => {
const spy = spyOn(console, 'warn');

const fooBarReducer = createReducer(
[],
on(foo, state => state),
on(bar, state => state),
on(foo, state => state)
);

expect(spy).toHaveBeenCalledTimes(1);
});

it('should warn in dev mode with multiple actions', () => {
const spy = spyOn(console, 'warn');

const fooBarReducer = createReducer(
[],
on(foo, foo, state => state),
on(bar, state => state)
);

expect(spy).toHaveBeenCalledTimes(1);
});

it('should not warn in prod mode', () => {
spyOn(ngCore, 'isDevMode').and.returnValue(false);
const spy = spyOn(console, 'warn');

const fooBarReducer = createReducer(
[],
on(foo, state => state),
on(bar, state => state),
on(foo, state => state)
);

expect(spy).not.toHaveBeenCalled();
});
});
});
});
});
9 changes: 1 addition & 8 deletions modules/store/src/reducer_creator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isDevMode } from '@angular/core';
import { ActionCreator, ActionReducer, ActionType, Action } from './models';

// Return type of the `on` fn.
Expand Down Expand Up @@ -185,18 +184,12 @@ export function createReducer<S>(
...ons: On<S>[]
): ActionReducer<S> {
const map = new Map<string, ActionReducer<S>>();
const devMode = isDevMode();

for (let on of ons) {
for (let type of on.types) {
if (devMode && map.has(type)) {
console.warn(
`@ngrx/store: The provided action type '${type}' is already provided.`
);
}
map.set(type, on.reducer);
}
}

return function(state: S = initialState, action: Action): S {
const reducer = map.get(action.type);
return reducer ? reducer(state, action) : state;
Expand Down

0 comments on commit b07ae4e

Please sign in to comment.