Forked from bertrandg/ngrx-effects-advance-example.js
Created
February 20, 2018 11:22
-
-
Save talamaska/31bc33db68e6247ec9ca9e27d6befcb5 to your computer and use it in GitHub Desktop.
ngrx/effects advance example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Nothing changed here, works as previously. | |
@Effect() actionX$ = this.updates$ | |
.ofType('ACTION_X') | |
.map(toPayload) | |
.switchMap(payload => this.api.callApiX(payload) | |
.map(data => ({type: 'ACTION_X_SUCCESS', payload: data})) | |
.catch(err => Observable.of({type: 'ACTION_X_FAIL', payload: err})) | |
); | |
// Here is the magic. | |
@Effect() actionY$ = this.updates$ | |
.ofType('ACTION_Y') | |
.map(toPayload) | |
// Retrieve part of the current state telling us if callApiX has been called already. | |
.withLatestFrom(this.store.select(state => state.someBoolean)) | |
.switchMap(([payload, someBoolean]) => { | |
// Function calling callApiY() and acting accordingly. | |
const callHttpY = v => { | |
return this.api.callApiY(v) | |
.map(data => ({type: 'ACTION_Y_SUCCESS', payload: data})) | |
.catch(err => Observable.of({type: 'ACTION_Y_FAIL', payload: err})); | |
} | |
// If data from store indicates that callApiX() has already been called with success | |
// Then directly call callApiY(). | |
if(someBoolean) { | |
return callHttpY(payload); | |
} | |
// Otherwise emit action triggering callApiX() | |
// Then wait for first response action (success or fail) | |
// And act accordingly. | |
return Observable.of({type: 'ACTION_X', payload}) | |
.merge( | |
this.updates$ | |
.ofType('ACTION_X_SUCCESS', 'ACTION_X_FAIL') | |
.first() | |
.switchMap(action => { | |
if(action.type === 'ACTION_X_FAIL') { | |
return Observable.of({type: 'ACTION_Y_FAIL', payload: 'Because ACTION_X failed.'}); | |
} | |
return callHttpY(payload); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment