Skip to content

Commit 36671cb

Browse files
potential fix of #107
1 parent 68be768 commit 36671cb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 2.3.0
44

5+
* `doWhile` now will guarantee that it will not call `doOnce` more than once for time
6+
57
## 2.2.9
68

79
* `Version`:

src/commonMain/kotlin/Executes.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,18 @@ suspend inline fun <T> doOnceTz(
7171
* Will execute [block] while it will return true as a result of its calculation
7272
*/
7373
suspend inline fun KronScheduler.doWhile(block: (DateTime) -> Boolean) {
74+
var latest: DateTime? = null
7475
do {
7576
delay(1L)
76-
} while (doOnce(block))
77+
val result = doOnce {
78+
if (latest != it) {
79+
latest = it
80+
block(it)
81+
} else {
82+
null
83+
}
84+
}
85+
} while (result == null || result)
7786
}
7887
/**
7988
* 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
8594
* Will execute [block] while it will return true as a result of its calculation
8695
*/
8796
suspend inline fun KronScheduler.doWhileTz(noinline block: suspend (DateTimeTz) -> Boolean) {
97+
var latest: DateTimeTz? = null
8898
do {
8999
delay(1L)
90-
} while (doOnceTz(block))
100+
val result = doOnceTz {
101+
if (latest != it) {
102+
latest = it
103+
block(it)
104+
} else {
105+
null
106+
}
107+
}
108+
} while (result == null || result)
91109
}
92110

93111
/**

0 commit comments

Comments
 (0)