Created
December 27, 2018 17:33
-
-
Save NathanSass/0266ae7d6829332fc9f15be3bbc3e2d6 to your computer and use it in GitHub Desktop.
Example of a fancy zip chain
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
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