Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created July 30, 2023 00:58
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 AdamMc331/ff0f9229dcd42d1bf7de8c7d5bd3a100 to your computer and use it in GitHub Desktop.
Save AdamMc331/ff0f9229dcd42d1bf7de8c7d5bd3a100 to your computer and use it in GitHub Desktop.
Interval Flowables
private fun buildFlowableFromIntervals(
intervals: List<Interval>,
): Flowable<ApiResponse> {
val intervalFlowables = intervals.map(::buildFlowableForInterval)
val completeFlowable = Flowable.concat(intervalFlowables)
return completeFlowable
.flatMap {
apiSingle().toFlowable()
}
}
private fun buildFlowableForInterval(
interval: Interval,
): Flowable<Long> {
val intervalFlowable = Flowable
.interval(
interval.intervalSeconds.toLong(),
TimeUnit.SECONDS,
)
// If duration seconds is not positive, emit indefinitely
return if (interval.durationSeconds <= 0) {
intervalFlowable
} else {
val totalIntervals = interval.durationSeconds / interval.intervalSeconds
intervalFlowable.take(totalIntervals.toLong())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment