diff --git a/modules/store/src/reducer_creator.ts b/modules/store/src/reducer_creator.ts index 4f75040a82..6c219b7734 100644 --- a/modules/store/src/reducer_creator.ts +++ b/modules/store/src/reducer_creator.ts @@ -1,4 +1,5 @@ import { ActionCreator, ActionReducer, ActionType, Action } from './models'; +import { compose } from './utils'; // Return type of the `on` fn. export interface On { @@ -229,25 +230,22 @@ export function createReducer( initialState: S, ...ons: On[] ): ActionReducer { - const map = new Map[]>(); + const map = new Map>(); for (let on of ons) { for (let type of on.types) { if (map.has(type)) { - const existingReducers = map.get(type) as ActionReducer[]; - map.set(type, [...existingReducers, on.reducer]); + 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]); + map.set(type, on.reducer); } } } return function(state: S = initialState, action: A): S { - const reducers = map.get(action.type); - return reducers && reducers.length > 0 - ? reducers.reduce( - (previousState, reducer) => reducer(previousState, action), - state - ) - : state; + const reducer = map.get(action.type); + return reducer ? reducer(state, action) : state; }; }