diff --git a/modules/store/spec/reducer_creator.spec.ts b/modules/store/spec/reducer_creator.spec.ts index ff2a2d4e7a..c7593128f8 100644 --- a/modules/store/spec/reducer_creator.spec.ts +++ b/modules/store/spec/reducer_creator.spec.ts @@ -86,6 +86,23 @@ import {on} from './modules/store/src/reducer_creator'; state = fooBarReducer(state, bar({ bar: 54 })); expect(state).toEqual(['[foobar] FOO', '[foobar] BAR']); }); + + it('should support "on"s to have identical action types', () => { + const increase = createAction('[COUNTER] increase'); + + const counterReducer = createReducer( + 0, + on(increase, state => state + 1), + on(increase, state => state + 1) + ); + + expect(typeof counterReducer).toEqual('function'); + + let state = 5; + + state = counterReducer(state, increase()); + expect(state).toEqual(7); + }); }); }); }); diff --git a/modules/store/src/reducer_creator.ts b/modules/store/src/reducer_creator.ts index ba22d2a93d..ce5ca0730d 100644 --- a/modules/store/src/reducer_creator.ts +++ b/modules/store/src/reducer_creator.ts @@ -232,7 +232,14 @@ export function createReducer( const map = new Map>(); for (let on of ons) { for (let type of on.types) { - map.set(type, on.reducer); + if (map.has(type)) { + const existingReducer = map.get(type) as ActionReducer; + const newReducer: ActionReducer = (state, action) => + on.reducer(existingReducer(state, action), action); + map.set(type, newReducer); + } else { + map.set(type, on.reducer); + } } }