Last active
November 25, 2019 08:12
-
-
Save hexadeciman/284d891d67aed0910834d4599ed6bc7c to your computer and use it in GitHub Desktop.
Chaining RXJS
This file contains 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
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