Skip to content

Instantly share code, notes, and snippets.

@luks91
Created October 16, 2017 00:57
Show Gist options
  • Save luks91/4623b7a2fa5d6789e28ceadd1a39ea60 to your computer and use it in GitHub Desktop.
Save luks91/4623b7a2fa5d6789e28ceadd1a39ea60 to your computer and use it in GitHub Desktop.
fun <TData> pagedDataOf(nextPageStartingFrom: (start: Int) -> Flowable<PagedResponse<TData>>): Flowable<List<TData>> =
Flowables.generateWith({ 0 }) { start, emitter ->
nextPageStartingFrom(start)
.doOnNext {
it.apply {
values.takeIf { it.isNotEmpty() }?.let { emitter.onNext(it) }
isLastPage.takeIf { it }?.let { emitter.onComplete() }
}
}
.map { it.nextPageStart }
.blockingFirst()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment