Skip to content

Commit

Permalink
feat(effects): forRoot and forFeature accept spreaded array (#3638)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Nov 6, 2022
1 parent 4f61b63 commit 0eaa536
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 13 deletions.
66 changes: 66 additions & 0 deletions modules/effects/spec/types/effects_module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { expecter } from 'ts-snippet';
import { compilerOptions } from './utils';

describe('EffectsModule()', () => {
const expectSnippet = expecter(
(code) => `
import { EffectsModule } from '@ngrx/effects';
class EffectFixture {}
${code}`,
compilerOptions()
);

describe('forRoot', () => {
it('should compile without params', () => {
expectSnippet(`
EffectsModule.forRoot();
`).toSucceed();
});

it('should compile with a single param', () => {
expectSnippet(`
EffectsModule.forRoot(EffectFixture);
`).toSucceed();
});

it('should compile with an array param', () => {
expectSnippet(`
EffectsModule.forRoot([EffectFixture]);
`).toSucceed();
});

it('should compile with a spreaded array param', () => {
expectSnippet(`
EffectsModule.forRoot(EffectFixture, EffectFixture);
`).toSucceed();
});
});

describe('forFeature', () => {
it('should compile without params', () => {
expectSnippet(`
EffectsModule.forFeature();
`).toSucceed();
});

it('should compile with a single param', () => {
expectSnippet(`
EffectsModule.forFeature(EffectFixture);
`).toSucceed();
});

it('should compile with an array param', () => {
expectSnippet(`
EffectsModule.forFeature([EffectFixture]);
`).toSucceed();
});

it('should compile with a spreaded array param', () => {
expectSnippet(`
EffectsModule.forFeature(EffectFixture, EffectFixture);
`).toSucceed();
});
});
});
38 changes: 38 additions & 0 deletions modules/effects/spec/types/provide_effects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expecter } from 'ts-snippet';
import { compilerOptions } from './utils';

describe('provideEffects()', () => {
const expectSnippet = expecter(
(code) => `
import { provideEffects } from '@ngrx/effects';
class EffectFixture {}
${code}`,
compilerOptions()
);

it('should compile without params', () => {
expectSnippet(`
provideEffects();
`).toSucceed();
});

it('should compile with a single param', () => {
expectSnippet(`
provideEffects(EffectFixture);
`).toSucceed();
});

it('should compile with an array param', () => {
expectSnippet(`
provideEffects([EffectFixture]);
`).toSucceed();
});

it('should compile with a spreaded array param', () => {
expectSnippet(`
provideEffects(EffectFixture, EffectFixture);
`).toSucceed();
});
});
26 changes: 20 additions & 6 deletions modules/effects/src/effects_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ import {
@NgModule({})
export class EffectsModule {
static forFeature(
featureEffects: Type<unknown>[] = []
featureEffects: Type<unknown>[]
): ModuleWithProviders<EffectsFeatureModule>;
static forFeature(
...featureEffects: Type<unknown>[]
): ModuleWithProviders<EffectsFeatureModule>;
static forFeature(
...featureEffects: Type<unknown>[] | Type<unknown>[][]
): ModuleWithProviders<EffectsFeatureModule> {
const effects = featureEffects.flat();
return {
ngModule: EffectsFeatureModule,
providers: [
featureEffects,
effects,
{
provide: _FEATURE_EFFECTS,
multi: true,
useValue: featureEffects,
useValue: effects,
},
{
provide: USER_PROVIDED_EFFECTS,
Expand All @@ -41,15 +48,22 @@ export class EffectsModule {
}

static forRoot(
rootEffects: Type<unknown>[] = []
rootEffects: Type<unknown>[]
): ModuleWithProviders<EffectsRootModule>;
static forRoot(
...rootEffects: Type<unknown>[]
): ModuleWithProviders<EffectsRootModule>;
static forRoot(
...rootEffects: Type<unknown>[] | Type<unknown>[][]
): ModuleWithProviders<EffectsRootModule> {
const effects = rootEffects.flat();
return {
ngModule: EffectsRootModule,
providers: [
rootEffects,
effects,
{
provide: _ROOT_EFFECTS,
useValue: [rootEffects],
useValue: [effects],
},
{
provide: _ROOT_EFFECTS_GUARD,
Expand Down
18 changes: 14 additions & 4 deletions modules/effects/src/provide_effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ import { rootEffectsInit as effectsInit } from './effects_actions';
/**
* Runs the provided effects.
* Can be called at the root and feature levels.
*
*/
export function provideEffects(effects: Type<unknown>[]): EnvironmentProviders;
/**
* Runs the provided effects.
* Can be called at the root and feature levels.
*/
export function provideEffects(
...effects: Type<unknown>[]
): EnvironmentProviders;
/**
* @usageNotes
*
* ### Providing effects at the root level
Expand All @@ -44,10 +53,11 @@ import { rootEffectsInit as effectsInit } from './effects_actions';
* ```
*/
export function provideEffects(
...effects: Type<unknown>[]
...effects: Type<unknown>[] | Type<unknown>[][]
): EnvironmentProviders {
const effectsFlattened = effects.flat();
return makeEnvironmentProviders([
effects,
effectsFlattened,
{
provide: ENVIRONMENT_INITIALIZER,
multi: true,
Expand All @@ -63,7 +73,7 @@ export function provideEffects(
effectsRunner.start();
}

for (const effectsClass of effects) {
for (const effectsClass of effectsFlattened) {
const effectsInstance = inject(effectsClass);
effectSources.addEffects(effectsInstance);
}
Expand Down
2 changes: 1 addition & 1 deletion projects/example-app/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { AppComponent } from '@example-app/core/containers';
*
* See: https://ngrx.io/guide/effects#registering-root-effects
*/
EffectsModule.forRoot([UserEffects, RouterEffects]),
EffectsModule.forRoot(UserEffects, RouterEffects),
CoreModule,
],
bootstrap: [AppComponent],
Expand Down
2 changes: 1 addition & 1 deletion projects/example-app/src/app/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const COMPONENTS = [
name: fromAuth.authFeatureKey,
reducer: fromAuth.reducers,
}),
EffectsModule.forFeature([AuthEffects]),
EffectsModule.forFeature(AuthEffects),
],
declarations: COMPONENTS,
})
Expand Down
2 changes: 1 addition & 1 deletion projects/example-app/src/app/books/books.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const CONTAINERS = [
* All Effects will only be instantiated once regardless of
* whether they are registered once or multiple times.
*/
EffectsModule.forFeature([BookEffects, CollectionEffects]),
EffectsModule.forFeature(BookEffects, CollectionEffects),
PipesModule,
],
declarations: [COMPONENTS, CONTAINERS],
Expand Down

0 comments on commit 0eaa536

Please sign in to comment.