Created
July 30, 2023 00:58
-
-
Save AdamMc331/ff0f9229dcd42d1bf7de8c7d5bd3a100 to your computer and use it in GitHub Desktop.
Interval Flowables
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
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