potential fix of #107

This commit is contained in:
InsanusMokrassar 2024-03-21 17:34:22 +06:00
parent 68be768763
commit 36671cb379
2 changed files with 22 additions and 2 deletions

View File

@ -2,6 +2,8 @@
## 2.3.0 ## 2.3.0
* `doWhile` now will guarantee that it will not call `doOnce` more than once for time
## 2.2.9 ## 2.2.9
* `Version`: * `Version`:

View File

@ -71,9 +71,18 @@ suspend inline fun <T> doOnceTz(
* 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(block: (DateTime) -> Boolean) { suspend inline fun KronScheduler.doWhile(block: (DateTime) -> Boolean) {
var latest: DateTime? = null
do { do {
delay(1L) delay(1L)
} while (doOnce(block)) val result = doOnce {
if (latest != it) {
latest = it
block(it)
} else {
null
}
}
} while (result == null || result)
} }
/** /**
* 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
@ -85,9 +94,18 @@ suspend inline fun KronScheduler.doWhileLocal(block: (DateTime) -> Boolean) = do
* 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.doWhileTz(noinline block: suspend (DateTimeTz) -> Boolean) { suspend inline fun KronScheduler.doWhileTz(noinline block: suspend (DateTimeTz) -> Boolean) {
var latest: DateTimeTz? = null
do { do {
delay(1L) delay(1L)
} while (doOnceTz(block)) val result = doOnceTz {
if (latest != it) {
latest = it
block(it)
} else {
null
}
}
} while (result == null || result)
} }
/** /**