4
4
ModuleWithProviders ,
5
5
OnDestroy ,
6
6
InjectionToken ,
7
+ Optional ,
7
8
} from '@angular/core' ;
8
9
import {
9
10
Action ,
@@ -17,11 +18,16 @@ import { compose, combineReducers, createReducerFactory } from './utils';
17
18
import {
18
19
INITIAL_STATE ,
19
20
INITIAL_REDUCERS ,
21
+ _INITIAL_REDUCERS ,
20
22
REDUCER_FACTORY ,
21
23
_REDUCER_FACTORY ,
22
24
STORE_FEATURES ,
23
25
_INITIAL_STATE ,
24
26
META_REDUCERS ,
27
+ _STORE_REDUCERS ,
28
+ FEATURE_REDUCERS ,
29
+ _FEATURE_REDUCERS ,
30
+ _FEATURE_REDUCERS_TOKEN ,
25
31
} from './tokens' ;
26
32
import { ACTIONS_SUBJECT_PROVIDERS , ActionsSubject } from './actions_subject' ;
27
33
import {
@@ -49,13 +55,19 @@ export class StoreRootModule {
49
55
export class StoreFeatureModule implements OnDestroy {
50
56
constructor (
51
57
@Inject ( STORE_FEATURES ) private features : StoreFeature < any , any > [ ] ,
58
+ @Inject ( FEATURE_REDUCERS ) private featureReducers : ActionReducerMap < any > [ ] ,
52
59
private reducerManager : ReducerManager
53
60
) {
54
61
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
+ } ;
59
71
} )
60
72
. forEach ( feature => reducerManager . addFeature ( feature ) ) ;
61
73
}
@@ -94,9 +106,18 @@ export class StoreModule {
94
106
useFactory : _initialStateFactory ,
95
107
deps : [ _INITIAL_STATE ] ,
96
108
} ,
109
+ { provide : _INITIAL_REDUCERS , useValue : reducers } ,
97
110
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
+ } ,
100
121
{
101
122
provide : META_REDUCERS ,
102
123
useValue : config . metaReducers ? config . metaReducers : [ ] ,
@@ -144,19 +165,52 @@ export class StoreModule {
144
165
multi : true ,
145
166
useValue : < StoreFeature < any , any > > {
146
167
key : featureName ,
147
- reducers : reducers ,
148
168
reducerFactory : config . reducerFactory
149
169
? config . reducerFactory
150
170
: combineReducers ,
151
171
metaReducers : config . metaReducers ? config . metaReducers : [ ] ,
152
172
initialState : config . initialState ,
153
173
} ,
154
174
} ,
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
+ } ,
155
191
] ,
156
192
} ;
157
193
}
158
194
}
159
195
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
+
160
214
export function _initialStateFactory ( initialState : any ) : any {
161
215
if ( typeof initialState === 'function' ) {
162
216
return initialState ( ) ;
0 commit comments