Skip to content

Instantly share code, notes, and snippets.

@mfp22
Last active March 23, 2021 08:56
Show Gist options
  • Select an option

  • Save mfp22/9ba134fe4ff2e36d890077fb54d73e64 to your computer and use it in GitHub Desktop.

Select an option

Save mfp22/9ba134fe4ff2e36d890077fb54d73e64 to your computer and use it in GitHub Desktop.
@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