I have an Angular (2) app with four ngrx actions:
The problem is that the Effect that catches ADVANCE seems to run before the reducer that processes SUCCESS
Here's the Effects code:
@Effect() start$ = this.actions$
.ofType('START')
.map(toPayload)
.switchMap(input => doAsyncTask(input)
.map(result => ({type: 'SUCCESS', payload: result}))
.catch(error => ({type: 'ERROR', payload: error})));
@Effect() success$ = this.actions$
.ofType('SUCCESS')
.map(() => ({type: 'ADVANCE'}));
@Effect({dispatch: false}) advance$ = this.actions$
.ofType('ADVANCE')
.withLatestFrom(this.store$.select(state => state.route))
.map(action_route => action_route[1])
.do(route => this.router.navigate([route.foo.bar]));
The error that I am getting is Cannot read property 'bar' of null
. The property foo
is set by the reducer that processes SUCCESS.
If I add a delay to the SUCCESS effect, it all works nicely:
@Effect() success$ = this.actions$
.ofType('SUCCESS')
.delay(1)
.map(() => ({type: 'ADVANCE'}));
But having to add this delay doesn't make sense to me.
I added console.log
statements everywhere and the output looks like this:
I expected the SUCCESS effect and the SUCCESS reducer to run before the ADVANCE effect.
Am I doing something wrong?
Is it incorrect to expect that actions are processed by the reducers in the same order that they are dispatched?
Versions:
To answer your concluding questions:
- Am I doing something wrong?
- Is it incorrect to expect that actions are processed by the reducers in the same order that they are dispatched?
There seems to be a bug causing this behaviour. You are most probably experiencing it since you are directly mapping to another action. Usually, if you have an asynchronous operation or something like that, the reducer has time to finish before the effect listening to the next action starts.
Perhaps not a part of your question, but a solution to your specified problem would be to navigate to your new route directly in SUCCESS with the help of your payload, or pass the payload to ADVANCE.
Links below are reported issues in effects-, store- and rxjs-projects that are related to yours:
Seems like they're working on it :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With