Skip to content

Instantly share code, notes, and snippets.

@LordRaydenMK
Last active January 9, 2021 09:43
Show Gist options
  • Save LordRaydenMK/b2aac18a7219a042a3017c8a9f4ce7a5 to your computer and use it in GitHub Desktop.
Save LordRaydenMK/b2aac18a7219a042a3017c8a9f4ce7a5 to your computer and use it in GitHub Desktop.
Unit tests and concurrency 1
// 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