krontab/src/commonMain/kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt

98 lines
3.6 KiB
Kotlin
Raw Normal View History

2020-11-21 08:58:19 +00:00
package dev.inmo.krontab.utils
2020-01-13 04:15:01 +00:00
import com.soywiz.klock.DateTime
2021-04-24 10:58:25 +00:00
import com.soywiz.klock.DateTimeTz
2023-03-18 06:23:53 +00:00
import com.soywiz.klock.milliseconds
import dev.inmo.krontab.*
2021-11-22 13:09:33 +00:00
import kotlinx.coroutines.FlowPreview
2023-03-18 06:23:53 +00:00
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
2021-11-22 13:09:33 +00:00
import kotlinx.coroutines.flow.Flow
2023-03-18 06:23:53 +00:00
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
2022-04-29 15:57:10 +00:00
import kotlinx.coroutines.isActive
2020-01-13 04:15:01 +00:00
2021-04-24 10:58:25 +00:00
/**
2023-03-18 06:23:53 +00:00
* **This flow is [cold](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/)**
2021-04-24 10:58:25 +00:00
*
2023-03-18 06:23:53 +00:00
* Will emit all the [KronScheduler.next] as soon as possible. In case [KronScheduler.next] return null, flow will
* be completed
*
* @param since Will be used as the first parameter for [KronScheduler.next] fun
2021-04-24 10:58:25 +00:00
*/
2020-01-13 04:15:01 +00:00
@FlowPreview
2023-03-18 06:23:53 +00:00
fun KronScheduler.asTzFlowWithoutDelays(since: DateTimeTz = DateTime.nowLocal()): Flow<DateTimeTz> = flow {
var previous = since
while (currentCoroutineContext().isActive) {
val next = next(previous) ?: break
emit(next)
previous = next + 1.milliseconds
2021-04-24 10:58:25 +00:00
}
}
/**
2023-03-18 06:23:53 +00:00
* **This flow is [cold](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/)**
*
* This [Flow] will use [asTzFlowWithoutDelays], but stop on each time until this time will happen
*/
@FlowPreview
fun KronScheduler.asTzFlowWithDelays(): Flow<DateTimeTz> = asTzFlowWithoutDelays().onEach { futureHappenTime ->
val now = DateTime.nowLocal()
delay((futureHappenTime - now).millisecondsLong)
}
/**
* **This flow is [cold](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/)**
*
* This [Flow] will use [asTzFlowWithoutDelays], but stop on each time until this time will happen
*/
@Deprecated(
"Behaviour will be changed. In some of near versions this flow will not delay executions",
ReplaceWith("this.asTzFlowWithDelays()", "dev.inmo.krontab.utils.asTzFlowWithDelays")
)
@FlowPreview
fun KronScheduler.asTzFlow(): Flow<DateTimeTz> = asTzFlowWithDelays()
/**
* **This flow is [cold](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/)**
*
2023-03-18 06:23:53 +00:00
* Will emit all the [KronScheduler.next] as soon as possible. In case [KronScheduler.next] return null, flow will
* be completed
*
* @param since Will be used as the first parameter for [KronScheduler.next] fun
*/
@FlowPreview
2023-03-18 06:23:53 +00:00
fun KronScheduler.asFlowWithoutDelays(since: DateTime = DateTime.now()): Flow<DateTime> = flow {
var previous = since
while (currentCoroutineContext().isActive) {
val next = next(previous) ?: break
emit(next)
previous = next + 1.milliseconds
}
}
2023-03-18 06:23:53 +00:00
/**
* **This flow is [cold](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/)**
*
* This [Flow] will use [asFlowWithoutDelays], but stop on each time until this time will happen
*/
@FlowPreview
fun KronScheduler.asFlowWithDelays(): Flow<DateTime> = asFlowWithoutDelays().onEach { futureHappenTime ->
val now = DateTime.now()
delay((futureHappenTime - now).millisecondsLong)
}
/**
* **This flow is [cold](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/)**
*
* This [Flow] will use [asFlowWithDelays], but stop on each time until this time will happen
*/
@Deprecated(
"Behaviour will be changed. In some of near versions this flow will not delay executions",
ReplaceWith("this.asFlowWithDelays()", "dev.inmo.krontab.utils.asFlowWithDelays")
)
@FlowPreview
fun KronScheduler.asFlow(): Flow<DateTime> = asFlowWithDelays()