Skip to content

Commit bd968fa

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Store): Use injector to get reducers provided via InjectionTokens (#259)
The injector behavior is different when compiled through AOT such that provided tokens aren't resolved before the factory function is executed. This fix uses the injector to get the reducers provided through tokens. Reference #189
1 parent c409252 commit bd968fa

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

modules/store/src/store_module.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
ModuleWithProviders,
55
OnDestroy,
66
InjectionToken,
7-
Optional,
7+
Injector,
88
} from '@angular/core';
99
import {
1010
Action,
@@ -114,10 +114,7 @@ export class StoreModule {
114114
},
115115
{
116116
provide: INITIAL_REDUCERS,
117-
deps: [
118-
_INITIAL_REDUCERS,
119-
[new Optional(), new Inject(_STORE_REDUCERS)],
120-
],
117+
deps: [Injector, _INITIAL_REDUCERS, [new Inject(_STORE_REDUCERS)]],
121118
useFactory: _createStoreReducers,
122119
},
123120
{
@@ -185,8 +182,9 @@ export class StoreModule {
185182
provide: FEATURE_REDUCERS,
186183
multi: true,
187184
deps: [
185+
Injector,
188186
_FEATURE_REDUCERS,
189-
[new Optional(), new Inject(_FEATURE_REDUCERS_TOKEN)],
187+
[new Inject(_FEATURE_REDUCERS_TOKEN)],
190188
],
191189
useFactory: _createFeatureReducers,
192190
},
@@ -196,20 +194,20 @@ export class StoreModule {
196194
}
197195

198196
export function _createStoreReducers(
197+
injector: Injector,
199198
reducers: ActionReducerMap<any, any>,
200199
tokenReducers: ActionReducerMap<any, any>
201200
) {
202-
return reducers instanceof InjectionToken ? tokenReducers : reducers;
201+
return reducers instanceof InjectionToken ? injector.get(reducers) : reducers;
203202
}
204203

205204
export function _createFeatureReducers(
205+
injector: Injector,
206206
reducerCollection: ActionReducerMap<any, any>[],
207207
tokenReducerCollection: ActionReducerMap<any, any>[]
208208
) {
209209
return reducerCollection.map((reducer, index) => {
210-
return reducer instanceof InjectionToken
211-
? tokenReducerCollection[index]
212-
: reducer;
210+
return reducer instanceof InjectionToken ? injector.get(reducer) : reducer;
213211
});
214212
}
215213

0 commit comments

Comments
 (0)