mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
delay in doWhile and flows with doWhile instead their own logic
This commit is contained in:
parent
539b5fbcd8
commit
4dc65bf09a
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
## 0.6.5
|
## 0.6.5
|
||||||
|
|
||||||
|
* Flows now use `doWhile` functions
|
||||||
|
* `doWhile` now use additional delay (for 1 ms) for cases when `block` executing too fast
|
||||||
|
|
||||||
## 0.6.4
|
## 0.6.4
|
||||||
|
|
||||||
* Versions
|
* Versions
|
||||||
|
@ -1,8 +1,23 @@
|
|||||||
package dev.inmo.krontab
|
package dev.inmo.krontab
|
||||||
|
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.DateTimeTz
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
|
||||||
|
*
|
||||||
|
* WARNING!!! If you want to launch it in parallel, you must do this explicitly.
|
||||||
|
*
|
||||||
|
* WARNING!!! In case if [KronScheduler.next] of [this] instance will return null, [block] will be called immediately
|
||||||
|
*/
|
||||||
|
suspend inline fun <T> KronSchedulerTz.doOnce(noinline block: suspend () -> T): T {
|
||||||
|
nextTimeZoned() ?.let {
|
||||||
|
delay((it - DateTimeTz.nowLocal()).millisecondsLong)
|
||||||
|
}
|
||||||
|
return block()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
|
* Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
|
||||||
*
|
*
|
||||||
@ -24,13 +39,31 @@ suspend inline fun <T> KronScheduler.doOnce(noinline block: suspend () -> T): T
|
|||||||
suspend inline fun <T> doOnce(
|
suspend inline fun <T> doOnce(
|
||||||
scheduleConfig: String,
|
scheduleConfig: String,
|
||||||
noinline block: suspend () -> T
|
noinline block: suspend () -> T
|
||||||
) = buildSchedule(scheduleConfig).doOnce(block)
|
) = with(buildSchedule(scheduleConfig)) {
|
||||||
|
when (this) {
|
||||||
|
is KronSchedulerTz -> doOnce(block)
|
||||||
|
else -> doOnce(block)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will execute [block] while it will return true as a result of its calculation
|
* Will execute [block] while it will return true as a result of its calculation
|
||||||
*/
|
*/
|
||||||
suspend inline fun KronScheduler.doWhile(noinline block: suspend () -> Boolean) {
|
suspend inline fun KronScheduler.doWhile(noinline block: suspend () -> Boolean) {
|
||||||
do { val doNext = doOnce(block) } while (doNext)
|
do {
|
||||||
|
delay(1L)
|
||||||
|
val doNext = doOnce(block)
|
||||||
|
} while (doNext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will execute [block] while it will return true as a result of its calculation
|
||||||
|
*/
|
||||||
|
suspend inline fun KronSchedulerTz.doWhile(noinline block: suspend () -> Boolean) {
|
||||||
|
do {
|
||||||
|
delay(1L)
|
||||||
|
val doNext = doOnce(block)
|
||||||
|
} while (doNext)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,7 +74,12 @@ suspend inline fun KronScheduler.doWhile(noinline block: suspend () -> Boolean)
|
|||||||
suspend inline fun doWhile(
|
suspend inline fun doWhile(
|
||||||
scheduleConfig: String,
|
scheduleConfig: String,
|
||||||
noinline block: suspend () -> Boolean
|
noinline block: suspend () -> Boolean
|
||||||
) = buildSchedule(scheduleConfig).doWhile(block)
|
) = with(buildSchedule(scheduleConfig)) {
|
||||||
|
when (this) {
|
||||||
|
is KronSchedulerTz -> doWhile(block)
|
||||||
|
else -> doWhile(block)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will execute [block] without any checking of result
|
* Will execute [block] without any checking of result
|
||||||
@ -50,6 +88,14 @@ suspend inline fun KronScheduler.doInfinity(noinline block: suspend () -> Unit)
|
|||||||
block()
|
block()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will execute [block] without any checking of result
|
||||||
|
*/
|
||||||
|
suspend inline fun KronSchedulerTz.doInfinity(noinline block: suspend () -> Unit) = doWhile {
|
||||||
|
block()
|
||||||
|
true
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Will [buildSchedule] using [scheduleConfig] and call [doInfinity] with [block]
|
* Will [buildSchedule] using [scheduleConfig] and call [doInfinity] with [block]
|
||||||
*
|
*
|
||||||
@ -58,4 +104,9 @@ suspend inline fun KronScheduler.doInfinity(noinline block: suspend () -> Unit)
|
|||||||
suspend inline fun doInfinity(
|
suspend inline fun doInfinity(
|
||||||
scheduleConfig: String,
|
scheduleConfig: String,
|
||||||
noinline block: suspend () -> Unit
|
noinline block: suspend () -> Unit
|
||||||
) = buildSchedule(scheduleConfig).doInfinity(block)
|
) = with(buildSchedule(scheduleConfig)) {
|
||||||
|
when (this) {
|
||||||
|
is KronSchedulerTz -> doInfinity(block)
|
||||||
|
else -> doInfinity(block)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,8 +2,7 @@ package dev.inmo.krontab.utils
|
|||||||
|
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
import com.soywiz.klock.DateTimeTz
|
import com.soywiz.klock.DateTimeTz
|
||||||
import dev.inmo.krontab.KronScheduler
|
import dev.inmo.krontab.*
|
||||||
import dev.inmo.krontab.next
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
@ -12,30 +11,43 @@ import kotlinx.coroutines.flow.*
|
|||||||
* time zones
|
* time zones
|
||||||
*
|
*
|
||||||
* @see channelFlow
|
* @see channelFlow
|
||||||
|
* @see KronSchedulerTz.doWhile
|
||||||
*/
|
*/
|
||||||
@FlowPreview
|
@FlowPreview
|
||||||
fun KronScheduler.asTzFlow(): Flow<DateTimeTz> = channelFlow {
|
fun KronSchedulerTz.asTzFlow(): Flow<DateTimeTz> = channelFlow {
|
||||||
var previousTime = DateTime.nowLocal()
|
doWhile {
|
||||||
while (isActive) {
|
send(DateTime.nowLocal())
|
||||||
val now = DateTime.nowLocal()
|
isActive
|
||||||
val nextTime = next(now) ?: break
|
|
||||||
if (previousTime == nextTime) {
|
|
||||||
delay(1L) // skip 1ms
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
previousTime = nextTime
|
|
||||||
}
|
|
||||||
val sleepDelay = (nextTime - DateTime.now().local).millisecondsLong
|
|
||||||
delay(sleepDelay)
|
|
||||||
send(nextTime)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is a map for [asTzFlow] and will works the same but return flow with [DateTime]s
|
* This method is a map for [asTzFlow] and will works the same but return flow with [DateTime]s
|
||||||
|
*
|
||||||
|
* @see channelFlow
|
||||||
|
* @see KronScheduler.doWhile
|
||||||
*/
|
*/
|
||||||
@FlowPreview
|
@FlowPreview
|
||||||
fun KronScheduler.asFlow(): Flow<DateTime> = asTzFlow().map { it.local }
|
fun KronScheduler.asFlow(): Flow<DateTime> = channelFlow {
|
||||||
|
doWhile {
|
||||||
|
send(DateTime.now())
|
||||||
|
isActive
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This [Flow] will trigger emitting each near time which will be returned from [this] [KronScheduler] with attention to
|
||||||
|
* time zones
|
||||||
|
*
|
||||||
|
* @see channelFlow
|
||||||
|
* @see KronScheduler.asFlow
|
||||||
|
* @see KronSchedulerTz.asTzFlow
|
||||||
|
*/
|
||||||
|
@FlowPreview
|
||||||
|
fun KronScheduler.asTzFlow(): Flow<DateTimeTz> = when (this) {
|
||||||
|
is KronSchedulerTz -> asTzFlow()
|
||||||
|
else -> asFlow().map { it.local }
|
||||||
|
}
|
||||||
|
|
||||||
@Deprecated(
|
@Deprecated(
|
||||||
"It is not recommended to use this class in future. This functionality will be removed soon",
|
"It is not recommended to use this class in future. This functionality will be removed soon",
|
||||||
|
Loading…
Reference in New Issue
Block a user