Skip to content

Commit

Permalink
chore: review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Aug 18, 2022
1 parent ca02fe3 commit 3182372
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
6 changes: 5 additions & 1 deletion modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ export {
StoreFeatureModule,
} from './store_module';
export { RootStoreConfig, StoreConfig, FeatureSlice } from './store_config';
export { provideStore, provideState } from './provide_store';
export {
provideStore,
provideState,
FEATURE_STATE_PROVIDER,
} from './provide_store';
export { ReducerTypes, on, createReducer } from './reducer_creator';
58 changes: 34 additions & 24 deletions modules/store/src/provide_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,47 @@ import {
_provideForRootGuard,
} from './store_config';

/**
* InjectionToken that registers feature states.
* Mainly used to provide a hook that can be injected
* to ensure feature state is loaded before something
* that depends on it.
*/
export const FEATURE_STATE_PROVIDER = new InjectionToken('NgRx Feature State', {
factory() {
const features = inject<StoreFeature<any, any>[]>(_STORE_FEATURES);
const featureReducers = inject<ActionReducerMap<any>[]>(FEATURE_REDUCERS);
const reducerManager = inject(ReducerManager);
inject(_ACTION_TYPE_UNIQUENESS_CHECK, InjectFlags.Optional);

const feats = features.map((feature, index) => {
const featureReducerCollection = featureReducers.shift();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const reducers = featureReducerCollection! /*TODO(#823)*/[index];

return {
...feature,
reducers,
initialState: _initialStateFactory(feature.initialState),
};
});

reducerManager.addFeatures(feats);
},
});

/**
* Environment Initializer used in the feature
* providers to register state features
*/
const ENVIRONMENT_STATE_PROVIDER: Provider[] = [
{ provide: FEATURE_STATE_PROVIDER, deps: [], useValue: true },
{
provide: ENVIRONMENT_INITIALIZER,
multi: true,
deps: [],
useFactory() {
return () => {
const features = inject<StoreFeature<any, any>[]>(_STORE_FEATURES);
const featureReducers =
inject<ActionReducerMap<any>[]>(FEATURE_REDUCERS);
const reducerManager = inject(ReducerManager);
inject(_ACTION_TYPE_UNIQUENESS_CHECK, InjectFlags.Optional);

const feats = features.map((feature, index) => {
const featureReducerCollection = featureReducers.shift();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const reducers = featureReducerCollection! /*TODO(#823)*/[index];

return {
...feature,
reducers,
initialState: _initialStateFactory(feature.initialState),
};
});

reducerManager.addFeatures(feats);
};
return () => inject(FEATURE_STATE_PROVIDER);
},
},
];
Expand Down Expand Up @@ -315,18 +325,18 @@ const ENVIRONMENT_STORE_PROVIDER = [
* ```
*/
export function provideStore<T, V extends Action = Action>(
reducers: ActionReducerMap<T, V> | InjectionToken<ActionReducerMap<T, V>>,
reducers?: ActionReducerMap<T, V> | InjectionToken<ActionReducerMap<T, V>>,
config?: RootStoreConfig<T, V>
): ImportedNgModuleProviders;
export function provideStore(
reducers:
reducers?:
| ActionReducerMap<any, any>
| InjectionToken<ActionReducerMap<any, any>>,
config: RootStoreConfig<any, any> = {}
): ImportedNgModuleProviders {
return {
ɵproviders: [
..._provideStore(reducers, config),
..._provideStore(reducers || {}, config),
ENVIRONMENT_STORE_PROVIDER,
],
};
Expand Down
2 changes: 1 addition & 1 deletion modules/store/src/store_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function _concatMetaReducers(
export function _provideForRootGuard(store: Store<any>): any {
if (store) {
throw new TypeError(
`StoreModule.forRoot() called twice. Feature modules should use StoreModule.forFeature() instead.`
`The root Store has been provided more than once. Feature modules should provide feature states instead.`
);
}
return 'guarded';
Expand Down

0 comments on commit 3182372

Please sign in to comment.