mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2026-03-23 09:23:31 +00:00
asFlow extension
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.insanusmokrassar.krontab.utils
|
||||
|
||||
import com.insanusmokrassar.krontab.KronScheduler
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
@FlowPreview
|
||||
fun KronScheduler.asFlow(): Flow<DateTime> = SchedulerFlow(this)
|
||||
|
||||
@FlowPreview
|
||||
class SchedulerFlow(
|
||||
private val scheduler: KronScheduler
|
||||
) : AbstractFlow<DateTime>() {
|
||||
@FlowPreview
|
||||
override suspend fun collectSafely(collector: FlowCollector<DateTime>) {
|
||||
while (true) {
|
||||
val now = DateTime.now()
|
||||
val nextTime = scheduler.next(now)
|
||||
val sleepDelay = (nextTime - now).millisecondsLong
|
||||
delay(sleepDelay)
|
||||
collector.emit(nextTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user