Last active
January 9, 2021 09:43
-
-
Save LordRaydenMK/b2aac18a7219a042a3017c8a9f4ce7a5 to your computer and use it in GitHub Desktop.
Unit tests and concurrency 1
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
// Emits Loading then Content or Problem | |
private fun requestData(): Observable<ViewState> = | |
service.firstApiCall() | |
.observeOn(Schedulers.computation()) | |
.map { it.message } | |
.flatMap(this::secondApiCall) | |
.map<ViewState> { ViewState.Content(it) } | |
.startWith(Single.just(ViewState.Loading)) | |
.onErrorReturn { ViewState.Problem } | |
.toObservable() | |
// Concurrently executes secondApiCall for each element in list. | |
// Transforms the result to ViewEntity, combines everything in a list | |
private fun secondApiCall(list: List<String>): Single<List<ViewEntity>> = | |
Observable.fromIterable(list) | |
.concatMapEager { item -> | |
service.secondEndpoint(item) | |
.observeOn(Schedulers.computation()) | |
.map { it.message } | |
.map { ViewEntity(item, it.toHttpUrl()) } | |
.onErrorReturn { DogViewEntity(item, null) } | |
.toObservable() | |
} | |
.toList() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment