Last active
March 23, 2021 08:56
-
-
Save mfp22/9ba134fe4ff2e36d890077fb54d73e64 to your computer and use it in GitHub Desktop.
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
| @Effect() actionX$ = this.updates$.pipe( | |
| ofType('ACTION_X'), | |
| map(toPayload), | |
| switchMap(payload => this.api.callApiX(payload).pipe( | |
| map(data => ({type: 'ACTION_X_SUCCESS', payload: data})), | |
| catch(err => Observable.of({type: 'ACTION_X_FAIL', payload: err})), | |
| )), | |
| ); | |
| @Effect() actionY$ = this.updates$.pipe( | |
| ofType('ACTION_Y'), | |
| map(toPayload), | |
| withLatestFrom(this.store.select(state => state.someBoolean)), | |
| switchMap(([payload, someBoolean]) => { | |
| const callHttpY = v => { | |
| return this.api.callApiY(v).pipe( | |
| map(data => ({ | |
| type: 'ACTION_Y_SUCCESS', | |
| payload: data | |
| })), | |
| catch(err => Observable.of({ | |
| type: 'ACTION_Y_FAIL', | |
| payload: err | |
| })), | |
| ); | |
| }; | |
| if(someBoolean) { | |
| return callHttpY(payload); | |
| } | |
| return merge( | |
| of({type: 'ACTION_X', payload}), | |
| this.updates$.pipe( | |
| ofType('ACTION_X_SUCCESS', 'ACTION_X_FAIL'), | |
| first(), | |
| switchMap(action => { | |
| if(action.type === 'ACTION_X_FAIL') { | |
| return 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