Skip to content

Commit

Permalink
feat(store): support meta reducer factory for feature (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itay committed Nov 25, 2018
1 parent ccd163d commit d6f7348
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export {
_INITIAL_STATE,
META_REDUCERS,
_STORE_REDUCERS,
_STORE_FEATURES,
_FEATURE_REDUCERS,
FEATURE_REDUCERS,
_FEATURE_REDUCERS_TOKEN,
Expand Down
27 changes: 25 additions & 2 deletions modules/store/src/store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -56,7 +57,7 @@ export class StoreRootModule {
@NgModule({})
export class StoreFeatureModule implements OnDestroy {
constructor(
@Inject(STORE_FEATURES) private features: StoreFeature<any, any>[],
@Inject(_STORE_FEATURES) private features: StoreFeature<any, any>[],
@Inject(FEATURE_REDUCERS) private featureReducers: ActionReducerMap<any>[],
private reducerManager: ReducerManager,
root: StoreRootModule
Expand All @@ -83,7 +84,7 @@ export class StoreFeatureModule implements OnDestroy {
export type StoreConfig<T, V extends Action = Action> = {
initialState?: InitialState<T>;
reducerFactory?: ActionReducerFactory<T, V>;
metaReducers?: MetaReducer<T, V>[];
metaReducers?: MetaReducer<T, V>[] | InjectionToken<MetaReducer<T, V>[]>;
};

@NgModule({})
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -206,6 +213,22 @@ export function _createStoreReducers(
return reducers instanceof InjectionToken ? injector.get(reducers) : reducers;
}

export function _createFeatureStore(
injector: Injector,
storeConfigs: StoreFeature<any, any>[]
) {
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<any, any>[],
Expand Down
5 changes: 5 additions & 0 deletions modules/store/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down

0 comments on commit d6f7348

Please sign in to comment.