Skip to content

Commit 0eaa536

Browse files
feat(effects): forRoot and forFeature accept spreaded array (#3638)
1 parent 4f61b63 commit 0eaa536

File tree

7 files changed

+141
-13
lines changed

7 files changed

+141
-13
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { expecter } from 'ts-snippet';
2+
import { compilerOptions } from './utils';
3+
4+
describe('EffectsModule()', () => {
5+
const expectSnippet = expecter(
6+
(code) => `
7+
import { EffectsModule } from '@ngrx/effects';
8+
9+
class EffectFixture {}
10+
11+
${code}`,
12+
compilerOptions()
13+
);
14+
15+
describe('forRoot', () => {
16+
it('should compile without params', () => {
17+
expectSnippet(`
18+
EffectsModule.forRoot();
19+
`).toSucceed();
20+
});
21+
22+
it('should compile with a single param', () => {
23+
expectSnippet(`
24+
EffectsModule.forRoot(EffectFixture);
25+
`).toSucceed();
26+
});
27+
28+
it('should compile with an array param', () => {
29+
expectSnippet(`
30+
EffectsModule.forRoot([EffectFixture]);
31+
`).toSucceed();
32+
});
33+
34+
it('should compile with a spreaded array param', () => {
35+
expectSnippet(`
36+
EffectsModule.forRoot(EffectFixture, EffectFixture);
37+
`).toSucceed();
38+
});
39+
});
40+
41+
describe('forFeature', () => {
42+
it('should compile without params', () => {
43+
expectSnippet(`
44+
EffectsModule.forFeature();
45+
`).toSucceed();
46+
});
47+
48+
it('should compile with a single param', () => {
49+
expectSnippet(`
50+
EffectsModule.forFeature(EffectFixture);
51+
`).toSucceed();
52+
});
53+
54+
it('should compile with an array param', () => {
55+
expectSnippet(`
56+
EffectsModule.forFeature([EffectFixture]);
57+
`).toSucceed();
58+
});
59+
60+
it('should compile with a spreaded array param', () => {
61+
expectSnippet(`
62+
EffectsModule.forFeature(EffectFixture, EffectFixture);
63+
`).toSucceed();
64+
});
65+
});
66+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expecter } from 'ts-snippet';
2+
import { compilerOptions } from './utils';
3+
4+
describe('provideEffects()', () => {
5+
const expectSnippet = expecter(
6+
(code) => `
7+
import { provideEffects } from '@ngrx/effects';
8+
9+
class EffectFixture {}
10+
11+
${code}`,
12+
compilerOptions()
13+
);
14+
15+
it('should compile without params', () => {
16+
expectSnippet(`
17+
provideEffects();
18+
`).toSucceed();
19+
});
20+
21+
it('should compile with a single param', () => {
22+
expectSnippet(`
23+
provideEffects(EffectFixture);
24+
`).toSucceed();
25+
});
26+
27+
it('should compile with an array param', () => {
28+
expectSnippet(`
29+
provideEffects([EffectFixture]);
30+
`).toSucceed();
31+
});
32+
33+
it('should compile with a spreaded array param', () => {
34+
expectSnippet(`
35+
provideEffects(EffectFixture, EffectFixture);
36+
`).toSucceed();
37+
});
38+
});

modules/effects/src/effects_module.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ import {
1414
@NgModule({})
1515
export class EffectsModule {
1616
static forFeature(
17-
featureEffects: Type<unknown>[] = []
17+
featureEffects: Type<unknown>[]
18+
): ModuleWithProviders<EffectsFeatureModule>;
19+
static forFeature(
20+
...featureEffects: Type<unknown>[]
21+
): ModuleWithProviders<EffectsFeatureModule>;
22+
static forFeature(
23+
...featureEffects: Type<unknown>[] | Type<unknown>[][]
1824
): ModuleWithProviders<EffectsFeatureModule> {
25+
const effects = featureEffects.flat();
1926
return {
2027
ngModule: EffectsFeatureModule,
2128
providers: [
22-
featureEffects,
29+
effects,
2330
{
2431
provide: _FEATURE_EFFECTS,
2532
multi: true,
26-
useValue: featureEffects,
33+
useValue: effects,
2734
},
2835
{
2936
provide: USER_PROVIDED_EFFECTS,
@@ -41,15 +48,22 @@ export class EffectsModule {
4148
}
4249

4350
static forRoot(
44-
rootEffects: Type<unknown>[] = []
51+
rootEffects: Type<unknown>[]
52+
): ModuleWithProviders<EffectsRootModule>;
53+
static forRoot(
54+
...rootEffects: Type<unknown>[]
55+
): ModuleWithProviders<EffectsRootModule>;
56+
static forRoot(
57+
...rootEffects: Type<unknown>[] | Type<unknown>[][]
4558
): ModuleWithProviders<EffectsRootModule> {
59+
const effects = rootEffects.flat();
4660
return {
4761
ngModule: EffectsRootModule,
4862
providers: [
49-
rootEffects,
63+
effects,
5064
{
5165
provide: _ROOT_EFFECTS,
52-
useValue: [rootEffects],
66+
useValue: [effects],
5367
},
5468
{
5569
provide: _ROOT_EFFECTS_GUARD,

modules/effects/src/provide_effects.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ import { rootEffectsInit as effectsInit } from './effects_actions';
1717
/**
1818
* Runs the provided effects.
1919
* Can be called at the root and feature levels.
20-
*
20+
*/
21+
export function provideEffects(effects: Type<unknown>[]): EnvironmentProviders;
22+
/**
23+
* Runs the provided effects.
24+
* Can be called at the root and feature levels.
25+
*/
26+
export function provideEffects(
27+
...effects: Type<unknown>[]
28+
): EnvironmentProviders;
29+
/**
2130
* @usageNotes
2231
*
2332
* ### Providing effects at the root level
@@ -44,10 +53,11 @@ import { rootEffectsInit as effectsInit } from './effects_actions';
4453
* ```
4554
*/
4655
export function provideEffects(
47-
...effects: Type<unknown>[]
56+
...effects: Type<unknown>[] | Type<unknown>[][]
4857
): EnvironmentProviders {
58+
const effectsFlattened = effects.flat();
4959
return makeEnvironmentProviders([
50-
effects,
60+
effectsFlattened,
5161
{
5262
provide: ENVIRONMENT_INITIALIZER,
5363
multi: true,
@@ -63,7 +73,7 @@ export function provideEffects(
6373
effectsRunner.start();
6474
}
6575

66-
for (const effectsClass of effects) {
76+
for (const effectsClass of effectsFlattened) {
6777
const effectsInstance = inject(effectsClass);
6878
effectSources.addEffects(effectsInstance);
6979
}

projects/example-app/src/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import { AppComponent } from '@example-app/core/containers';
7474
*
7575
* See: https://ngrx.io/guide/effects#registering-root-effects
7676
*/
77-
EffectsModule.forRoot([UserEffects, RouterEffects]),
77+
EffectsModule.forRoot(UserEffects, RouterEffects),
7878
CoreModule,
7979
],
8080
bootstrap: [AppComponent],

projects/example-app/src/app/auth/auth.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const COMPONENTS = [
3030
name: fromAuth.authFeatureKey,
3131
reducer: fromAuth.reducers,
3232
}),
33-
EffectsModule.forFeature([AuthEffects]),
33+
EffectsModule.forFeature(AuthEffects),
3434
],
3535
declarations: COMPONENTS,
3636
})

projects/example-app/src/app/books/books.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const CONTAINERS = [
6161
* All Effects will only be instantiated once regardless of
6262
* whether they are registered once or multiple times.
6363
*/
64-
EffectsModule.forFeature([BookEffects, CollectionEffects]),
64+
EffectsModule.forFeature(BookEffects, CollectionEffects),
6565
PipesModule,
6666
],
6767
declarations: [COMPONENTS, CONTAINERS],

0 commit comments

Comments
 (0)