I have an Angular (2) app with four ngrx actions:
- START
- Not processed by the reducer (no state change)
- ngrx Effect calls an async task and maps to SUCCESS or ERROR
- SUCCESS
- Processed by the reducer
- ngrx Effect maps to ADVANCE
- ADVANCE
- Not processed by the reducer
- ngrx Effect navigates to a different route
- ERROR
- Processed by the reducer
- No Effect
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:
- SUCCESS effect
- ADVANCE effect (showing route.foo === null)
- SUCCESS reducer (showing route.foo === something)
- Error
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:
- @angular/cli: 1.0.0-beta.32.3
- node: 7.5.0
- os: darwin x64
- @angular/common: 2.4.7
- @angular/compiler: 2.4.7
- @angular/core: 2.4.7
- @angular/forms: 2.4.7
- @angular/http: 2.4.7
- @angular/platform-browser: 2.4.7
- @angular/platform-browser-dynamic: 2.4.7
- @angular/router: 3.4.7
- @angular/cli: 1.0.0-beta.32.3
- @angular/compiler-cli: 2.4.7
- @ngrx/[email protected]
- @ngrx/[email protected]
- @ngrx/[email protected]
- rxjs: 5.1.1
@ngrx
versions are the same. What about your RxJS version? Mine is 5.2.0. You might also want to include yourNgModule
bootstrapping in the question. - cartant