optimization and docs of executes

This commit is contained in:
InsanusMokrassar 2020-06-03 21:53:55 +06:00
parent eb11235360
commit c9243d7968

View File

@ -1,32 +1,53 @@
package com.insanusmokrassar.krontab package com.insanusmokrassar.krontab
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
suspend inline fun KronScheduler.doWhile(noinline block: suspend () -> Boolean) { /**
do { * Execute [block] once at the [KronScheduler.next] time and return result of [block] calculation.
delay((next() - DateTime.now()).millisecondsLong) *
} while (block()) * WARNING!!! If you want to launch it in parallel, you must do this explicit.
*/
suspend inline fun <T> KronScheduler.doOnce(noinline block: suspend () -> T): T {
delay((next() - DateTime.now()).millisecondsLong)
return block()
} }
/**
* Will [createSimpleScheduler] using [scheduleConfig] and call [doOnce] on it
*/
suspend inline fun <T> doOnce(
scheduleConfig: String,
noinline block: suspend () -> T
) = createSimpleScheduler(scheduleConfig).doOnce(block)
/**
* Will execute [block] while it will return true as a result of its calculation
*/
suspend inline fun KronScheduler.doWhile(noinline block: suspend () -> Boolean) {
do { val doNext = doOnce(block) } while (doNext)
}
/**
* Will [createSimpleScheduler] using [scheduleConfig] and call [doWhile] with [block]
*/
suspend inline fun doWhile( suspend inline fun doWhile(
scheduleConfig: String, scheduleConfig: String,
noinline block: suspend () -> Boolean noinline block: suspend () -> Boolean
) = createSimpleScheduler(scheduleConfig).doWhile(block) ) = createSimpleScheduler(scheduleConfig).doWhile(block)
/**
* Will execute [block] without any checking of result
*/
suspend inline fun KronScheduler.doInfinity(noinline block: suspend () -> Unit) = doWhile { suspend inline fun KronScheduler.doInfinity(noinline block: suspend () -> Unit) = doWhile {
block() block()
true true
} }
/**
* Will [createSimpleScheduler] using [scheduleConfig] and call [doInfinity] with [block]
*/
suspend inline fun doInfinity( suspend inline fun doInfinity(
scheduleConfig: String, scheduleConfig: String,
noinline block: suspend () -> Unit noinline block: suspend () -> Unit
) = createSimpleScheduler(scheduleConfig).doInfinity(block) ) = createSimpleScheduler(scheduleConfig).doInfinity(block)
suspend inline fun KronScheduler.doOnce(noinline block: suspend () -> Unit) = doWhile {
block()
false
}
suspend inline fun doOnce(
scheduleConfig: String,
noinline block: suspend () -> Unit
) = createSimpleScheduler(scheduleConfig).doOnce(block)