Skip to content

Commit 7082fba

Browse files
committed
Add isComplete to AsyncCallback.Barrier
Closes #818
1 parent 8d75648 commit 7082fba

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

core/src/main/scala/japgolly/scalajs/react/AsyncCallback.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ object AsyncCallback {
3131
p <- SyncPromise[A]
3232
} yield (AsyncCallback(p.onComplete), p.complete)
3333

34-
final case class Barrier(await: AsyncCallback[Unit], complete: Callback) {
34+
final class Barrier(val await: AsyncCallback[Unit], completePromise: Callback) {
35+
36+
private var _complete = false
37+
38+
def complete: Callback =
39+
completePromise.finallyRun(Callback { _complete = true })
40+
41+
def isComplete: CallbackTo[Boolean] =
42+
CallbackTo(_complete)
3543

3644
@inline
3745
@deprecated("Use .await", "1.7.7")
@@ -43,7 +51,7 @@ object AsyncCallback {
4351
lazy val barrier: CallbackTo[Barrier] =
4452
for {
4553
(promise, complete) <- promise[Unit]
46-
} yield Barrier(promise, complete(tryUnit))
54+
} yield new Barrier(promise, complete(tryUnit))
4755

4856
def first[A](f: (Try[A] => Callback) => Callback): AsyncCallback[A] =
4957
new AsyncCallback(g => CallbackTo {

doc/changelog/1.7.7.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
## 1.7.7
1+
# 1.7.7
22

33
This entire release is focused on `AsyncCallback`.
44

5-
* In `AsyncCallback.Barrier`, renamed `waitForCompletion` to `await`.
6-
`waitForCompletion` is still available but will result in a deprecation warning.
7-
Automatic migration is below.
5+
* `AsyncCallback.Barrier`:
86

7+
* Added `isComplete: CallbackTo[Boolean]` to synchronously query whether the barrier is complete or not.
98

10-
### Migration
9+
* Renamed `waitForCompletion` to `await`.
10+
`waitForCompletion` is still available but will result in a deprecation warning.
11+
Automatic migration is below.
12+
13+
14+
## Migration
1115

1216
```sh
1317
find . -type f -name '*.scala' -exec perl -pi -e '

test/src/test/scala/japgolly/scalajs/react/core/AsyncCallbackTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ object AsyncCallbackTest extends TestSuite {
108108
}
109109
}
110110

111+
"barrier" - {
112+
val b = AsyncCallback.barrier.runNow()
113+
assertEq(b.isComplete.runNow(), false)
114+
b.complete.runNow()
115+
assertEq(b.isComplete.runNow(), true)
116+
}
117+
111118
"debounce" - {
112119
val t = new TestTimer
113120
var i = 0

0 commit comments

Comments
 (0)