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

Init effect is not triggering other effects #152

Closed
berwyn opened this issue Jul 21, 2017 · 14 comments
Closed

Init effect is not triggering other effects #152

berwyn opened this issue Jul 21, 2017 · 14 comments

Comments

@berwyn
Copy link

berwyn commented Jul 21, 2017

I'm in the process of migrating my app from ngrx 2 -> 4 and I'm having some issues with the init migration.

Previously I had:

@Effect()
init(): Observable<Action> {
  return this.actions$
    .ofType('@ngrx/effects/init')
    .mergeMap(() => Observable.of(doSetup()));
}

Which I converted, based on the migration guide, to:

@Effect()
init(): Observable<Action> {
  return Observable.defer(() => Observable.of(doSetup()));
}

in either case, I have another effect in the same class that's listening for the type returned by doInit:

@Effect()
onSetup(): Observable<Action> {
  return this.actions$
    .ofType(ActionTypes.doSetup)
    .mergeMap(action => {
      // contents elided
    });
}

and this effect is not triggered at all. The action from init is dispatched, but isn't caught by onSetup, which is causing my app to stop working :/. There are no errors logged or any other diagnostic information I'm seeing, just the action being dispatched but not caught by the effect.

@maxisam
Copy link
Contributor

maxisam commented Jul 22, 2017

duplicated #103

@berwyn
Copy link
Author

berwyn commented Jul 24, 2017

I used #103 (and the update to the readme it triggered) to update my code to use defer. The problem here is that the effect that uses defer is trying to kick off other effects, and these secondary effects don't get triggered at all.

In the example given, init is dispatching an action that onSetup is listening for, but onSetup never activates. It's registered, as observed by dropping a breakpoint and stepping through, but the body of the mergeMap is never invoked.

@maxisam
Copy link
Contributor

maxisam commented Jul 24, 2017

Move the init effect, the one with defer, to the bottom of the file. The sequence matters. I think we have discussed this in #103

@Spawnrad
Copy link

Same as @maxisam I can confirm, that after moving the init$ with defer to the bottom, applicationInit$ is executed, BUT other actions, dispatched from component, using this.store.dispatch(new MyAction()); are not handled by effects.

@berwyn
Copy link
Author

berwyn commented Jul 27, 2017

So, I moved the defer and that worked as @maxisam explained, however that only seems to solve effects running in the same class. In my case, I need to kick off effects in multiple classes on init. Do I just need to refactor all of those to run on init themselves?

@maxisam
Copy link
Contributor

maxisam commented Jul 27, 2017

@berwyn I was in the same boat as you. So I refactor my code to use initialValue instead since my case is more like filling up initial values.

@noemi-salaun
Copy link

With the defer at the end of the classe, it is executed when the application init, but the other effects in the same classe are not triggered when there respective actions are dispatched.

@brandonroberts
Copy link
Member

This issue should be resolved in the latest release of @ngrx/effects. If that's not the case, please open a new issue.

@austbot
Copy link

austbot commented Oct 11, 2017

The only way I can get this to work with the most current effects @brandonroberts is to use .delay on the .of in the deferred observable

@felikf
Copy link

felikf commented Oct 24, 2017

@brandonroberts the latest (10/24) 4.1.0 version of @ngrx/effects suffers from the same issue; I had to move the defer(...) at the bottom.

@maxisam
Copy link
Contributor

maxisam commented Oct 24, 2017

@felikf have you tried the new ROOT_EFFECTS_INIT ?

need to import it like
import { Actions, Effect, ROOT_EFFECTS_INIT } from '@ngrx/effects';

It should work.

@biesbjerg
Copy link

@maxisam / others: It seems to work when listening for ROOT_EFFECTS_INIT for effects that are registered in AppModule with forRoot, but what about when we register effects using forFeature in sub modules? They're not ready when ROOT_EFFECTS_INIT fires.

That makes sense, when you take the name ROOT_EFFECTS_INIT into consideration, but I'm not sure what is the correct way to make it work like it did in 2.x? Any ideas?

@kevindqc
Copy link

Maybe you could use the startWith operator?

@danday74
Copy link

In case this helps someone this solved my problem:

Changing:

import { defer, of } from 'rxjs/index' // suggested by IDE

to

import { defer, of } from 'rxjs'

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

No branches or pull requests

10 participants