-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
The way to fire an effect after effects are settled up #246
Comments
Not sure why this would be needed. Have you tried the following operator: this.onInit$ = this.actions$
.ofType(Actions.REESTABLISH)
.startWith(new Actions.ReestablishAction()) // This triggers the specified action on load.
.switchMap((isAuthorized) => {
return this.isAuthorized$;
})
.map((isAuthorized) => {
if (isAuthorized) {
this.store.dispatch({ type: '[posts] getAll' })
}
}); |
@wesselvdv Also, I don't think, that I need to initialize first value in stream with I can write that in the service constructor this.isAuthorized$
.take(1)
.subscribe({
next: (isAuthorized) => {
if (isAuthorized) {
this.store.dispatch({ type: '[posts] getAll' })
}
},
});
|
@ValeryVS Those are placeholder action classes. You're using strings as actions, while the accepted way to define an action is using a class with a type parameter that contains a string. Something like this from the example app: export class LoginAction implements Action {
readonly type = LOGIN;
constructor(public payload: Authenticate) {}
} Doing it like above gives you type checking on the payload. I just used a placeholder that follows that convention. Anyhow, back on topic; where is isAuthorized coming from? Is that an initial state defined in a reducer? What is it? |
@wesselvdv Now I wrote this @Effect({ dispatch: false })
this.onInit$ = this.actions$
.ofType('[app] init')
.switchMap(() => this.isAuthorized$)
.map((isAuthorized) => {
if (isAuthorized) {
this.accountService.fetchAll();
this.imgService.fetchAll();
this.personService.fetchAll();
this.roleService.fetchAll();
}
});
public init() {
this.store.dispatch({ type: '[app] init' });
} It works, but I need to laucn I suggest to make action, that will be dispatched automatically. |
That's way too complicated for this. The only thing you want is the initial state of a reducer, and you want to fire a boatload of actions based off it. You can get the current state of a reducer using an operator: @Effect()
this.onInit$ = this.actions$
.ofType('[app] init')
.startWith('[app] init')
.withLatestFrom(this.store.select(state => state.isAuthorized)) // < ---- Not sure where your prop is exactly.
.map(([, isAuthorized]) => {
if (isAuthorized) {
this.accountService.fetchAll();
this.imgService.fetchAll();
this.personService.fetchAll();
this.roleService.fetchAll();
}
}); Where you'll need to inject the store in your constructor as you would normally do to access a state from anywhere. |
I will accept a PR that adds an "Root Effects Init" action that gets dispatched after the root effects have been started. |
@tdeschryver any movement on this? |
@brandonroberts Didn't change anything since last time. |
@tdeschryver yes, that's fine. |
I'm submitting a...
What is the current behavior?
There is no action, no function which fired when all effects are settled up. Where we can dispatch actions to fire other effects.
Expected behavior:
or, may be
Minimal reproduction of the problem with instructions:
There a few issues, of
@ngrx/store/init
action. Seems, that this action is not useful for described purpose anymore. So, we probably need another.#103
#152
Version of affected browser(s),operating system(s), npm, node and ngrx:
ngrx v4
Other information:
Possible workaround:
dispatch action, somewhere after application start.
In the
AppComponent
for example.And use it to dispatch other actions, that must be dispatched after all effects are settled up.
The text was updated successfully, but these errors were encountered: