Reputation: 393
I am having some trouble chaining actions one after the other in an Effect that makes an HTTP-request.
Here's the Effect code:
export class UserEffects {
@Effect()
update$: Observable<Action> = this.actions$.ofType(user.USERCHANGE).pipe(
switchMap(data => this.authService.login(data['payload'])),
map(userInfo => new UserChangedAction(userInfo)),
tap(() => this.store.dispatch(
new LoginStateChangeAction(localStorage.getItem('token')))
)
);
constructor(private authService: AuthService, private actions$: Actions,
public store: Store<fromRoot.State>) {}
}
The problem is both the actions are being called simultaneously. The LoginStateChange action depends on UserChanged action to complete.
How can I achieve this?
Thanks!
Upvotes: 8
Views: 7197
Reputation: 762
You can try such combination - UserChangedAction.switchMapTo(LoginStateChangeAction).pipe(stuff)
. It will wait for UserChangedAction
emit and pnly after that will wait for LoginStateChangeAction
emit. So it will be one by one.
Upvotes: 0
Reputation: 15497
You can return multiple actions as explained in Austin's post: Dispatching Multiple Actions from NGRX Effects
@Effect()
update$ = this.actions$.ofType(user.USERCHANGE).pipe(
switchMap(data => this.authService.login(data['payload'])),
switchMap(userInfo => [
new UserChangedAction(userInfo),
new LoginStateChangeAction(localStorage.getItem('token')),
])
);
Upvotes: 9