Skip to content

Commit

Permalink
feat(store): add object-style StoreModule.forFeature overload w/fixes (
Browse files Browse the repository at this point in the history
…#2885)

* feat(store): add object-style StoreModule.forFeature overload (#2821)

Closes #2809

* fix(store): update StoreModule to be compliant with AOT

Co-authored-by: Suguru Inatomi <[email protected]>
  • Loading branch information
brandonroberts and lacolaco authored Jan 17, 2021
1 parent 2f5dcb4 commit a9468e1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
30 changes: 30 additions & 0 deletions modules/store/spec/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,34 @@ describe(`Store Modules`, () => {
});
});
});

describe(`: With slice object`, () => {
@NgModule({
imports: [
StoreModule.forFeature({ name: 'a', reducer: featureAReducer }),
],
})
class FeatureAModule {}

@NgModule({
imports: [StoreModule.forRoot({}), FeatureAModule],
})
class RootModule {}

beforeEach(() => {
TestBed.configureTestingModule({
imports: [RootModule],
});

store = TestBed.inject(Store);
});

it('should set up a feature state', () => {
store.pipe(take(1)).subscribe((state: State) => {
expect(state).toEqual({
a: 5,
} as State);
});
});
});
});
1 change: 1 addition & 0 deletions modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ export {
StoreFeatureModule,
RootStoreConfig,
StoreConfig,
FeatureSlice,
} from './store_module';
export { On, on, createReducer } from './reducer_creator';
40 changes: 33 additions & 7 deletions modules/store/src/store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ export interface RootStoreConfig<T, V extends Action = Action>
runtimeChecks?: Partial<RuntimeChecks>;
}

/**
* An object with the name and the reducer for the feature.
*/
export interface FeatureSlice<T, V extends Action = Action> {
name: string;
reducer: ActionReducer<T, V>;
}

@NgModule({})
export class StoreModule {
static forRoot<T, V extends Action = Action>(
Expand Down Expand Up @@ -192,13 +200,19 @@ export class StoreModule {
reducer: ActionReducer<T, V> | InjectionToken<ActionReducer<T, V>>,
config?: StoreConfig<T, V> | InjectionToken<StoreConfig<T, V>>
): ModuleWithProviders<StoreFeatureModule>;
static forFeature<T, V extends Action = Action>(
slice: FeatureSlice<T, V>,
config?: StoreConfig<T, V> | InjectionToken<StoreConfig<T, V>>
): ModuleWithProviders<StoreFeatureModule>;
static forFeature(
featureName: string,
reducers:
featureNameOrSlice: string | FeatureSlice<any, any>,
reducersOrConfig?:
| ActionReducerMap<any, any>
| InjectionToken<ActionReducerMap<any, any>>
| ActionReducer<any, any>
| InjectionToken<ActionReducer<any, any>>,
| InjectionToken<ActionReducer<any, any>>
| StoreConfig<any, any>
| InjectionToken<StoreConfig<any, any>>,
config: StoreConfig<any, any> | InjectionToken<StoreConfig<any, any>> = {}
): ModuleWithProviders<StoreFeatureModule> {
return {
Expand All @@ -207,13 +221,16 @@ export class StoreModule {
{
provide: _FEATURE_CONFIGS,
multi: true,
useValue: config,
useValue: typeof featureNameOrSlice === 'string' ? config : {},
},
{
provide: STORE_FEATURES,
multi: true,
useValue: {
key: featureName,
key:
typeof featureNameOrSlice === 'string'
? featureNameOrSlice
: featureNameOrSlice.name,
reducerFactory:
!(config instanceof InjectionToken) && config.reducerFactory
? config.reducerFactory
Expand All @@ -233,12 +250,21 @@ export class StoreModule {
deps: [Injector, _FEATURE_CONFIGS, STORE_FEATURES],
useFactory: _createFeatureStore,
},
{ provide: _FEATURE_REDUCERS, multi: true, useValue: reducers },
{
provide: _FEATURE_REDUCERS,
multi: true,
useValue:
typeof featureNameOrSlice === 'string'
? reducersOrConfig
: featureNameOrSlice.reducer,
},
{
provide: _FEATURE_REDUCERS_TOKEN,
multi: true,
useExisting:
reducers instanceof InjectionToken ? reducers : _FEATURE_REDUCERS,
reducersOrConfig instanceof InjectionToken
? reducersOrConfig
: _FEATURE_REDUCERS,
},
{
provide: FEATURE_REDUCERS,
Expand Down
5 changes: 4 additions & 1 deletion projects/example-app/src/app/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const COMPONENTS = [
ReactiveFormsModule,
MaterialModule,
AuthRoutingModule,
StoreModule.forFeature(fromAuth.authFeatureKey, fromAuth.reducers),
StoreModule.forFeature({
name: fromAuth.authFeatureKey,
reducer: fromAuth.reducers,
}),
EffectsModule.forFeature([AuthEffects]),
],
declarations: COMPONENTS,
Expand Down

0 comments on commit a9468e1

Please sign in to comment.