From d6f7348fa635ac3a3fdea76c872bdd5986808931 Mon Sep 17 00:00:00 2001 From: Itay Date: Sat, 24 Nov 2018 09:41:48 +0200 Subject: [PATCH] feat(store): support meta reducer factory for feature (#1414) --- modules/store/src/index.ts | 1 + modules/store/src/store_module.ts | 27 +++++++++++++++++++++++++-- modules/store/src/tokens.ts | 5 +++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/modules/store/src/index.ts b/modules/store/src/index.ts index ce02914c93..24c71ef076 100644 --- a/modules/store/src/index.ts +++ b/modules/store/src/index.ts @@ -40,6 +40,7 @@ export { _INITIAL_STATE, META_REDUCERS, _STORE_REDUCERS, + _STORE_FEATURES, _FEATURE_REDUCERS, FEATURE_REDUCERS, _FEATURE_REDUCERS_TOKEN, diff --git a/modules/store/src/store_module.ts b/modules/store/src/store_module.ts index 4f0b7a3544..48424e9963 100644 --- a/modules/store/src/store_module.ts +++ b/modules/store/src/store_module.ts @@ -29,6 +29,7 @@ import { FEATURE_REDUCERS, _FEATURE_REDUCERS, _FEATURE_REDUCERS_TOKEN, + _STORE_FEATURES, } from './tokens'; import { ACTIONS_SUBJECT_PROVIDERS, ActionsSubject } from './actions_subject'; import { @@ -56,7 +57,7 @@ export class StoreRootModule { @NgModule({}) export class StoreFeatureModule implements OnDestroy { constructor( - @Inject(STORE_FEATURES) private features: StoreFeature[], + @Inject(_STORE_FEATURES) private features: StoreFeature[], @Inject(FEATURE_REDUCERS) private featureReducers: ActionReducerMap[], private reducerManager: ReducerManager, root: StoreRootModule @@ -83,7 +84,7 @@ export class StoreFeatureModule implements OnDestroy { export type StoreConfig = { initialState?: InitialState; reducerFactory?: ActionReducerFactory; - metaReducers?: MetaReducer[]; + metaReducers?: MetaReducer[] | InjectionToken[]>; }; @NgModule({}) @@ -176,6 +177,12 @@ export class StoreModule { initialState: config.initialState, }, }, + { + provide: _STORE_FEATURES, + multi: false, + deps: [Injector, STORE_FEATURES], + useFactory: _createFeatureStore, + }, { provide: _FEATURE_REDUCERS, multi: true, useValue: reducers }, { provide: _FEATURE_REDUCERS_TOKEN, @@ -206,6 +213,22 @@ export function _createStoreReducers( return reducers instanceof InjectionToken ? injector.get(reducers) : reducers; } +export function _createFeatureStore( + injector: Injector, + storeConfigs: StoreFeature[] +) { + return storeConfigs.map(storeConfig => { + const metaReducers = storeConfig.metaReducers; + return { + ...storeConfig, + metaReducers: + metaReducers instanceof InjectionToken + ? injector.get(metaReducers) + : metaReducers, + }; + }); +} + export function _createFeatureReducers( injector: Injector, reducerCollection: ActionReducerMap[], diff --git a/modules/store/src/tokens.ts b/modules/store/src/tokens.ts index 7e2bd233aa..0426a45ada 100644 --- a/modules/store/src/tokens.ts +++ b/modules/store/src/tokens.ts @@ -24,6 +24,11 @@ export const _STORE_REDUCERS = new InjectionToken( export const _FEATURE_REDUCERS = new InjectionToken( '@ngrx/store Internal Feature Reducers' ); + +export const _STORE_FEATURES = new InjectionToken( + '@ngrx/store Internal Store Features' +); + export const _FEATURE_REDUCERS_TOKEN = new InjectionToken( '@ngrx/store Internal Feature Reducers Token' );