44 ModuleWithProviders ,
55 OnDestroy ,
66 InjectionToken ,
7+ Optional ,
78} from '@angular/core' ;
89import {
910 Action ,
@@ -17,11 +18,16 @@ import { compose, combineReducers, createReducerFactory } from './utils';
1718import {
1819 INITIAL_STATE ,
1920 INITIAL_REDUCERS ,
21+ _INITIAL_REDUCERS ,
2022 REDUCER_FACTORY ,
2123 _REDUCER_FACTORY ,
2224 STORE_FEATURES ,
2325 _INITIAL_STATE ,
2426 META_REDUCERS ,
27+ _STORE_REDUCERS ,
28+ FEATURE_REDUCERS ,
29+ _FEATURE_REDUCERS ,
30+ _FEATURE_REDUCERS_TOKEN ,
2531} from './tokens' ;
2632import { ACTIONS_SUBJECT_PROVIDERS , ActionsSubject } from './actions_subject' ;
2733import {
@@ -49,13 +55,19 @@ export class StoreRootModule {
4955export class StoreFeatureModule implements OnDestroy {
5056 constructor (
5157 @Inject ( STORE_FEATURES ) private features : StoreFeature < any , any > [ ] ,
58+ @Inject ( FEATURE_REDUCERS ) private featureReducers : ActionReducerMap < any > [ ] ,
5259 private reducerManager : ReducerManager
5360 ) {
5461 features
55- . map ( feature => {
56- return typeof feature . initialState === 'function'
57- ? { ...feature , initialState : feature . initialState ( ) }
58- : feature ;
62+ . map ( ( feature , index ) => {
63+ const featureReducerCollection = featureReducers . shift ( ) ;
64+ const reducers = featureReducerCollection [ index ] ;
65+
66+ return {
67+ ...feature ,
68+ reducers,
69+ initialState : _initialStateFactory ( feature . initialState ) ,
70+ } ;
5971 } )
6072 . forEach ( feature => reducerManager . addFeature ( feature ) ) ;
6173 }
@@ -94,9 +106,18 @@ export class StoreModule {
94106 useFactory : _initialStateFactory ,
95107 deps : [ _INITIAL_STATE ] ,
96108 } ,
109+ { provide : _INITIAL_REDUCERS , useValue : reducers } ,
97110 reducers instanceof InjectionToken
98- ? { provide : INITIAL_REDUCERS , useExisting : reducers }
99- : { provide : INITIAL_REDUCERS , useValue : reducers } ,
111+ ? [ { provide : _STORE_REDUCERS , useExisting : reducers } ]
112+ : [ ] ,
113+ {
114+ provide : INITIAL_REDUCERS ,
115+ deps : [
116+ _INITIAL_REDUCERS ,
117+ [ new Optional ( ) , new Inject ( _STORE_REDUCERS ) ] ,
118+ ] ,
119+ useFactory : _createStoreReducers ,
120+ } ,
100121 {
101122 provide : META_REDUCERS ,
102123 useValue : config . metaReducers ? config . metaReducers : [ ] ,
@@ -144,19 +165,52 @@ export class StoreModule {
144165 multi : true ,
145166 useValue : < StoreFeature < any , any > > {
146167 key : featureName ,
147- reducers : reducers ,
148168 reducerFactory : config . reducerFactory
149169 ? config . reducerFactory
150170 : combineReducers ,
151171 metaReducers : config . metaReducers ? config . metaReducers : [ ] ,
152172 initialState : config . initialState ,
153173 } ,
154174 } ,
175+ { provide : _FEATURE_REDUCERS , multi : true , useValue : reducers } ,
176+ {
177+ provide : _FEATURE_REDUCERS_TOKEN ,
178+ multi : true ,
179+ useExisting :
180+ reducers instanceof InjectionToken ? reducers : _FEATURE_REDUCERS ,
181+ } ,
182+ {
183+ provide : FEATURE_REDUCERS ,
184+ multi : true ,
185+ deps : [
186+ _FEATURE_REDUCERS ,
187+ [ new Optional ( ) , new Inject ( _FEATURE_REDUCERS_TOKEN ) ] ,
188+ ] ,
189+ useFactory : _createFeatureReducers ,
190+ } ,
155191 ] ,
156192 } ;
157193 }
158194}
159195
196+ export function _createStoreReducers (
197+ reducers : ActionReducerMap < any , any > ,
198+ tokenReducers : ActionReducerMap < any , any >
199+ ) {
200+ return reducers instanceof InjectionToken ? tokenReducers : reducers ;
201+ }
202+
203+ export function _createFeatureReducers (
204+ reducerCollection : ActionReducerMap < any , any > [ ] ,
205+ tokenReducerCollection : ActionReducerMap < any , any > [ ]
206+ ) {
207+ return reducerCollection . map ( ( reducer , index ) => {
208+ return reducer instanceof InjectionToken
209+ ? tokenReducerCollection [ index ]
210+ : reducer ;
211+ } ) ;
212+ }
213+
160214export function _initialStateFactory ( initialState : any ) : any {
161215 if ( typeof initialState === 'function' ) {
162216 return initialState ( ) ;
0 commit comments