Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schematics: Default effect leads to an endless loop #1573

Closed
fmalcher opened this issue Feb 20, 2019 · 11 comments · Fixed by #1576
Closed

Schematics: Default effect leads to an endless loop #1573

fmalcher opened this issue Feb 20, 2019 · 11 comments · Fixed by #1576

Comments

@fmalcher
Copy link
Contributor

fmalcher commented Feb 20, 2019

The command ng generate feature does create an effects class with the following effect (example):

@Effect()
loadBooks$ = this.actions$.pipe(ofType(BookActionTypes.LoadBooks));

(see https://github.com/ngrx/platform/blob/master/modules/schematics/src/effect/files/__name%40dasherize%40if-flat__/__name%40dasherize__.effects.ts#L24)

If left untouched, this will lead to a loop of death as soon as the first LoadBooks action is being dispatched.

Possible solutions

  • Comment out the @Effect() decorator by default
  • don't provide a default effect after creation
  • set { dispatch: false }

I don't like any of them – but I ran into that the infinite loop problem multiple times now 😆
Will prepare a PR if there is consensus about how we can solve this.

I would be willing to submit a PR to fix this issue

[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

@timdeschryver
Copy link
Member

timdeschryver commented Feb 20, 2019

We recently merged PR #1530 which resolves this issue 😅 .
It will become available in the next version.

@brandonroberts
Copy link
Member

#1530 was released with 7.2.0 👋

@timdeschryver
Copy link
Member

The docs aren't reflecting these changes, would it be OK to open up a new issue to add the api flag introduced in the PR?

@fmalcher
Copy link
Contributor Author

fmalcher commented Feb 20, 2019

Thanks for the quick reply! However, I'm not sure #1530 actually fixes this.

With ng g feature foobar --api I actually get a "harmless" effect –
but ng g feature foobar still produces this one:

@Injectable()
export class FoobarEffects {

  @Effect()
  loadFoobars$ = this.actions$.pipe(ofType(FoobarActionTypes.LoadFoobars));

  constructor(private actions$: Actions) {}
}

Version is 7.2.0 installed today 😇

What do you think about completely removing the sample effect if the --api flag is not used?

@brandonroberts
Copy link
Member

It's easier to delete the code than to write it from scratch. I think we should at least add a note in the docs that the generated effect must be updated to return a new action.

I don't usually dispatch the action until the effects are wired up but I can see how it causes an issue.

@timdeschryver
Copy link
Member

You're right @fmalcher.

We discussed the possible solutions in the issues thread, #1524 (comment).

With @fmalcher's proposed solutions in mind, should we re-open this issue or leave this as is. If we re-open the issue do we still agree that commenting out the effect is the best solution?

@brandonroberts
Copy link
Member

I want to put developers on the path of continuing with minor modifications. I'd recommend we do something similar to the --api and concatMap to an EMPTY observable by default.

@fmalcher
Copy link
Contributor Author

fmalcher commented Feb 20, 2019

We discussed the possible solutions in the issues thread, #1524 (comment).

Ah I see! Haven't seen this before -- this would have made THIS whole issue obsolete 🙈 sorry

I'd recommend we do something similar to the --api and concatMap to an EMPTY observable by default.

Sounds good to me! I could live with both ways:

(1) concatMap to EMPTY

@Effect()
loadFoobars$ = this.actions$.pipe(
  ofType(FoobarActionTypes.LoadFoobars),
  concatMap(() => EMPTY) // remove or replace with your own side effect execution
);

(2) comment out

// @Effect()
// loadFoobars$ = this.actions$.pipe(ofType(FoobarActionTypes.LoadFoobars));

As I find (1) a sensible solution, should I submit a PR for this?
@brandonroberts @timdeschryver

@fmalcher
Copy link
Contributor Author

fmalcher commented Feb 20, 2019

Slightly related:
ng g feature foobar --api injects Actions in the new typed way:

private actions$: Actions<FoobarActions>

while ng g feature foobar still uses the old way where we need to type ofType explicitly:

private actions$: Actions

Am I right assuming that this is not intended and should be changed as well? 😅

@timdeschryver
Copy link
Member

I would also prefer option one 😄

The reason why ng g feature foobar doesn't use the new way, is because we don't need to infer the payload of the action. But perhaps it would be better to still type the actions? 🤔 .

@brandonroberts
Copy link
Member

It should still be typed when generating a feature because we have an actions union.

brandonroberts pushed a commit that referenced this issue Feb 26, 2019
…tic (#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
tja4472 pushed a commit to tja4472/platform that referenced this issue Mar 20, 2019
…tic (ngrx#1576)

- Make the generated effect for 'ng g feature'  harmless by not dispatching any action.
- Type the 'Actions' Injectable by default in effect

Closes ngrx#1573
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants