Skip to content

Commit 5fbcb3c

Browse files
fmalcherbrandonroberts
authored andcommitted
fix(schematics): type actions and avoid endless loop in effect schematic (#1576)
- Make the generated effect for 'ng g feature' harmless by not dispatching any action. - Type the 'Actions' Injectable by default in effect Closes #1573
1 parent 1bba710 commit 5fbcb3c

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

modules/schematics/src/effect/files/__name@dasherize@if-flat__/__name@dasherize__.effects.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { Injectable } from '@angular/core';
22
import { Actions, Effect<% if (feature) { %>, ofType<% } %> } from '@ngrx/effects';
3-
<% if (feature && api) { %>import { catchError, map, concatMap } from 'rxjs/operators';<% } %>
4-
<% if (feature && api) { %>import { EMPTY, of } from 'rxjs';<% } %>
5-
<% if (feature && api) { %>import { Load<%= classify(name) %>sFailure, Load<%= classify(name) %>sSuccess, <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
6-
<% if (feature && !api) { %>import { <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
3+
<% if (feature && api) { %>import { catchError, map, concatMap } from 'rxjs/operators';
4+
import { EMPTY, of } from 'rxjs';
5+
import { Load<%= classify(name) %>sFailure, Load<%= classify(name) %>sSuccess, <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
6+
<% } %>
7+
<% if (feature && !api) { %>import { concatMap } from 'rxjs/operators';
8+
import { EMPTY } from 'rxjs';
9+
import { <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
10+
<% } %>
711

812
@Injectable()
913
export class <%= classify(name) %>Effects {
@@ -17,13 +21,16 @@ export class <%= classify(name) %>Effects {
1721
map(data => new Load<%= classify(name) %>sSuccess({ data })),
1822
catchError(error => of(new Load<%= classify(name) %>sFailure({ error }))))
1923
)
20-
);
21-
<% } %>
24+
);<% } %>
2225
<% if (feature && !api) { %>
2326
@Effect()
24-
load<%= classify(name) %>s$ = this.actions$.pipe(ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s));
27+
load<%= classify(name) %>s$ = this.actions$.pipe(
28+
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s),
29+
/** An EMPTY observable only emits completion. Replace with your own observable API request */
30+
concatMap(() => EMPTY)
31+
);
2532
<% } %>
26-
<% if (feature && api) { %>
33+
<% if (feature) { %>
2734
constructor(private actions$: Actions<<%= classify(name) %>Actions>) {}
2835
<% } else { %>
2936
constructor(private actions$: Actions) {}

modules/schematics/src/effect/index.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe('Effect Schematic', () => {
236236
);
237237

238238
expect(content).toMatch(
239-
/import \{ FooActionTypes } from \'\.\.\/\.\.\/actions\/foo\/foo\.actions';/
239+
/import \{ FooActionTypes, FooActions } from \'\.\.\/\.\.\/actions\/foo\/foo\.actions';/
240240
);
241241
});
242242

@@ -250,12 +250,18 @@ describe('Effect Schematic', () => {
250250
expect(content).toMatch(
251251
/import { Actions, Effect, ofType } from '@ngrx\/effects';/
252252
);
253+
expect(content).toMatch(/import { concatMap } from 'rxjs\/operators';/);
254+
expect(content).toMatch(/import { EMPTY } from 'rxjs';/);
253255
expect(content).toMatch(
254-
/import { FooActionTypes } from '\.\/foo.actions';/
256+
/import { FooActionTypes, FooActions } from '\.\/foo.actions';/
255257
);
256258
expect(content).toMatch(/export class FooEffects/);
259+
expect(content).toMatch(/loadFoos\$ = this\.actions\$.pipe\(/);
260+
expect(content).toMatch(/ofType\(FooActionTypes\.LoadFoos\)/);
261+
expect(content).toMatch(/concatMap\(\(\) => EMPTY\)/);
262+
257263
expect(content).toMatch(
258-
/loadFoos\$ = this\.actions\$.pipe\(ofType\(FooActionTypes\.LoadFoos\)\);/
264+
/constructor\(private actions\$: Actions<FooActions>\) {}/
259265
);
260266
});
261267

@@ -274,7 +280,7 @@ describe('Effect Schematic', () => {
274280
);
275281
expect(content).toMatch(/export class FooEffects/);
276282
expect(content).not.toMatch(
277-
/loadFoos\$ = this\.actions\$.pipe\(ofType\(FooActionTypes\.LoadFoos\)\);/
283+
/loadFoos\$ = this\.actions\$.pipe\(ofType\(FooActionTypes\.LoadFoos/
278284
);
279285
});
280286

0 commit comments

Comments
 (0)