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 effects not triggering other effects #1250

Closed
jonrimmer opened this issue Aug 14, 2018 · 7 comments
Closed

Init effects not triggering other effects #1250

jonrimmer opened this issue Aug 14, 2018 · 7 comments

Comments

@jonrimmer
Copy link
Contributor

Minimal reproduction of the bug/regression with instructions:

Similar issue to #152

https://stackblitz.com/edit/ngrx-seed-fccfzh

Check the console.

  1. The 'LOGIN' action is dispatched at init.
  2. The 'FEATURE_ACTION' action should be dispatched in response.
  3. It isn't.

Expected behavior:

Init effects should trigger other effects.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):

Angular / NgRx: 6.1

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

This is because the feature's effect hasn't been loaded yet when the root effects are loaded.
What exactly are you trying to accomplish?
Because I think the behaviour of #683 would be what you need.

@jonrimmer
Copy link
Contributor Author

I want a feature effect triggered by user login actions, whenever they occur in the application's lifecycle. And I want to dispatch a login action when the application is initialized, if the user is already logged in.

Basically, I want to be able to defer init effects until all non-lazy loaded features have been loaded. E.g. when the app has actually finished initializing, not while it's still in the process of doing so. This seems like a pretty basic requirement!?

I don't really see how the linked issue would solve it. It would let individual features know that they've been loaded, but it wouldn't let the application as a whole know that all features have been loaded.

@timdeschryver
Copy link
Member

Because then you could do the following instead of using defer:

@Effect()
foo = this.actions.pipe(
  ofType('feature/init'),
  mapTo(new LoginAction())
);

@jonrimmer
Copy link
Contributor Author

OK, that only tells me me that one feature has initialised though. If there are more, which there will be, I'm sounds like I'm going to have include a registry of all the features and their init state in the store, updating them when I receive an init action, checking if they're all ready, then dispatching LoginAction only then.

I guess I can do that, although it feels like I'm adding a lot of app-level infrastructure to duplicate configuration and processing that ngrx/effects is presumably already handling. It already has a register of features, because I'm registering them using EffectsModule.forFeature(). It already knows which are initialised, and when they're all initialised, because it's responsible for initialising them. Given it's doing all this, and it already dispatches lifecycle actions like ROOT_EFFECTS_INIT, maybe it could dispatch an action called something like ALL_FEATURES_INIT when all features have been intialised?

@timdeschryver
Copy link
Member

I don't think adding ALL_FEATURES_INIT will help with lazy loaded modules.
Or you would have multiple actions of there fired, which could introduce some strange behaviors.

@jonrimmer
Copy link
Contributor Author

I guess it wouldn't, but it would help with statically loaded ones, which would still be an improvement on the current situation.

@brandonroberts
Copy link
Member

Fixed via #1305

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

No branches or pull requests

3 participants