Skip to content

Commit

Permalink
fix(Store): Use injector to get reducers provided via InjectionTokens (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Aug 10, 2017
1 parent c409252 commit bd968fa
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions modules/store/src/store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ModuleWithProviders,
OnDestroy,
InjectionToken,
Optional,
Injector,
} from '@angular/core';
import {
Action,
Expand Down Expand Up @@ -114,10 +114,7 @@ export class StoreModule {
},
{
provide: INITIAL_REDUCERS,
deps: [
_INITIAL_REDUCERS,
[new Optional(), new Inject(_STORE_REDUCERS)],
],
deps: [Injector, _INITIAL_REDUCERS, [new Inject(_STORE_REDUCERS)]],
useFactory: _createStoreReducers,
},
{
Expand Down Expand Up @@ -185,8 +182,9 @@ export class StoreModule {
provide: FEATURE_REDUCERS,
multi: true,
deps: [
Injector,
_FEATURE_REDUCERS,
[new Optional(), new Inject(_FEATURE_REDUCERS_TOKEN)],
[new Inject(_FEATURE_REDUCERS_TOKEN)],
],
useFactory: _createFeatureReducers,
},
Expand All @@ -196,20 +194,20 @@ export class StoreModule {
}

export function _createStoreReducers(
injector: Injector,
reducers: ActionReducerMap<any, any>,
tokenReducers: ActionReducerMap<any, any>
) {
return reducers instanceof InjectionToken ? tokenReducers : reducers;
return reducers instanceof InjectionToken ? injector.get(reducers) : reducers;
}

export function _createFeatureReducers(
injector: Injector,
reducerCollection: ActionReducerMap<any, any>[],
tokenReducerCollection: ActionReducerMap<any, any>[]
) {
return reducerCollection.map((reducer, index) => {
return reducer instanceof InjectionToken
? tokenReducerCollection[index]
: reducer;
return reducer instanceof InjectionToken ? injector.get(reducer) : reducer;
});
}

Expand Down

0 comments on commit bd968fa

Please sign in to comment.