From 685cb322609defc75c5a8c990551912d58371a6b Mon Sep 17 00:00:00 2001 From: Kern Zhao Date: Tue, 24 Sep 2019 10:08:07 -0400 Subject: [PATCH] compose reducer on the fly instead of storing reducer array --- modules/store/src/reducer_creator.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/store/src/reducer_creator.ts b/modules/store/src/reducer_creator.ts index 4f75040a82..e740ac3f94 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,14 +230,16 @@ 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); } } }