Skip to content

Instantly share code, notes, and snippets.

@hexadeciman
Last active November 25, 2019 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hexadeciman/284d891d67aed0910834d4599ed6bc7c to your computer and use it in GitHub Desktop.
Save hexadeciman/284d891d67aed0910834d4599ed6bc7c to your computer and use it in GitHub Desktop.
Chaining RXJS
const loginEpic = actions$ =>
actions$.ofType(ActionTypes.LOGIN_REQUESTED)
.switchMap(
(loginAction) => Observable.ajax(POSTOptions('/api/login', loginAction.credentials),
(action, r) => ([r, action])
)
.switchMap(
([loginResponse, action]) => Observable.ajax(
GETOptions('/api/provider', [loginResponse.response.userToken, action.serviceProviderId] )
)
)
.map(providerResponse => loginResolvedAction(providerResponse.response));
// Real Example
@Effect()
LoadMorePotatoesList = this.actions
.pipe(
ofType(new PotatoesActions.LoadMorePotatoesAction().type),
switchMap(() => {
return this.potatoesService.getPotatoeList(false).pipe(
switchMap(potatoeList => {
const FriesIds: string[] = this.getUniqueFriesIds(potatoeList);
return this.friesService.findPeoplesName(FriesIds).pipe(
map(Fries => {
Fries = this.mapFriesResponse(Fries);
const mappedPotatoes = potatoeList.map(potatoe => this.mapPotatoe(potatoe, Fries));
return new PotatoesActions.LoadMorePotatoesSuccessAction({
data: mappedPotatoes,
hasMore: potatoeList.length === SB_MAX_Potatoes_COUNT
});
}),
catchError(err => {
logErrorAndNotifyUser(this.notificationService, Potatoes_ERRORS.LOAD_MORE_Potatoes, err);
return observableOf(new PotatoesActions.LoadMorePotatoesFailAction(err));
})
)
}),
catchError(err => {
logErrorAndNotifyUser(this.notificationService, Potatoes_ERRORS.LOAD_MORE_Potatoes, err);
return observableOf(new PotatoesActions.LoadMorePotatoesFailAction(err));
})
)
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment