Skip to content

Instantly share code, notes, and snippets.

@NathanSass
Created December 27, 2018 17:33
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 NathanSass/0266ae7d6829332fc9f15be3bbc3e2d6 to your computer and use it in GitHub Desktop.
Save NathanSass/0266ae7d6829332fc9f15be3bbc3e2d6 to your computer and use it in GitHub Desktop.
Example of a fancy zip chain
override fun getDocumentAndAudiobook(docId: Int): Single<AudioData.LoaderResult> {
val licenseRequest = audioApi.getLicense(docId).toObservable()
val documentRequest = documentLoaderObservable(docId)
val requests = mutableListOf(licenseRequest, documentRequest)
return Observable.zip(requests,
Function<Array<Any>, CombinedResult> {
return@Function resolve(it)
})
.flatMap(Function<CombinedResult, Observable<Pair<ScribdDocument, FindawayApi.PlayListResponse>>> {
val playlistRequest = audioApi.getPlaylist(
faeDocId = it.document.audiobook.externalId.toInt(),
licenseId = it.license,
sessionKey = it.sessionKey).toObservable()
return@Function Observable.zip(
Observable.just(it.document),
playlistRequest,
BiFunction<ScribdDocument, FindawayApi.PlayListResponse, Pair<ScribdDocument, FindawayApi.PlayListResponse>> { document, playlist ->
return@BiFunction Pair(document, playlist)
})
})
.flatMap(Function<Pair<ScribdDocument, FindawayApi.PlayListResponse>, Observable<AudioData.LoaderResult>> {
val audiobook = buildAudiobook(it.first, it.second)
return@Function Observable.just(AudioData.LoaderResult(it.first, audiobook))
}).firstOrError()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment