Skip to content

Commit f378921

Browse files
committed
Refactor: move things around in AsyncCallback
1 parent 4eb8c5f commit f378921

File tree

1 file changed

+64
-55
lines changed

1 file changed

+64
-55
lines changed

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

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

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)
43-
44-
@inline
45-
@deprecated("Use .await", "1.7.7")
46-
def waitForCompletion: AsyncCallback[Unit] =
47-
await
48-
}
49-
50-
/** A synchronisation aid that allows you to wait for another async process to complete. */
51-
lazy val barrier: CallbackTo[Barrier] =
52-
for {
53-
(promise, complete) <- promise[Unit]
54-
} yield new Barrier(promise, complete(tryUnit))
55-
56-
final class CountDownLatch(count: Int, barrier: Barrier) {
57-
private var _pending = count.max(0)
58-
59-
/** Decrements the count of the latch, releasing all waiting computations if the count reaches zero. */
60-
val countDown: Callback =
61-
Callback {
62-
if (_pending > 0) {
63-
_pending -= 1
64-
if (_pending == 0) {
65-
barrier.complete.runNow()
66-
}
67-
}
68-
}
69-
70-
def await: AsyncCallback[Unit] =
71-
barrier.await
72-
73-
def isComplete: CallbackTo[Boolean] =
74-
barrier.isComplete
75-
76-
def pending: CallbackTo[Int] =
77-
CallbackTo(_pending)
78-
}
79-
80-
/** A synchronization aid that allows you to wait until a set of async processes completes. */
81-
def countDownLatch(count: Int): CallbackTo[CountDownLatch] =
82-
for {
83-
b <- barrier
84-
_ <- b.complete.when_(count <= 0)
85-
} yield new CountDownLatch(count, b)
86-
8734
def first[A](f: (Try[A] => Callback) => Callback): AsyncCallback[A] =
8835
new AsyncCallback(g => CallbackTo {
8936
var first = true
@@ -313,15 +260,77 @@ object AsyncCallback {
313260
}
314261
}
315262

316-
final case class Forked[A](await: AsyncCallback[A], isComplete: CallbackTo[Boolean])
317-
318263
def awaitAll(as: AsyncCallback[_]*): AsyncCallback[Unit] =
319264
if (as.isEmpty)
320265
unit
321266
else
322267
sequence_(as.iterator.asInstanceOf[Iterator[AsyncCallback[Any]]])
268+
269+
// ===================================================================================================================
270+
271+
final case class Forked[A](await: AsyncCallback[A], isComplete: CallbackTo[Boolean])
272+
273+
// ===================================================================================================================
274+
275+
final class Barrier(val await: AsyncCallback[Unit], completePromise: Callback) {
276+
277+
private var _complete = false
278+
279+
def complete: Callback =
280+
completePromise.finallyRun(Callback { _complete = true })
281+
282+
def isComplete: CallbackTo[Boolean] =
283+
CallbackTo(_complete)
284+
285+
@inline
286+
@deprecated("Use .await", "1.7.7")
287+
def waitForCompletion: AsyncCallback[Unit] =
288+
await
289+
}
290+
291+
/** A synchronisation aid that allows you to wait for another async process to complete. */
292+
lazy val barrier: CallbackTo[Barrier] =
293+
for {
294+
(promise, complete) <- promise[Unit]
295+
} yield new Barrier(promise, complete(tryUnit))
296+
297+
// ===================================================================================================================
298+
299+
final class CountDownLatch(count: Int, barrier: Barrier) {
300+
private var _pending = count.max(0)
301+
302+
/** Decrements the count of the latch, releasing all waiting computations if the count reaches zero. */
303+
val countDown: Callback =
304+
Callback {
305+
if (_pending > 0) {
306+
_pending -= 1
307+
if (_pending == 0) {
308+
barrier.complete.runNow()
309+
}
310+
}
311+
}
312+
313+
def await: AsyncCallback[Unit] =
314+
barrier.await
315+
316+
def isComplete: CallbackTo[Boolean] =
317+
barrier.isComplete
318+
319+
def pending: CallbackTo[Int] =
320+
CallbackTo(_pending)
321+
}
322+
323+
/** A synchronization aid that allows you to wait until a set of async processes completes. */
324+
def countDownLatch(count: Int): CallbackTo[CountDownLatch] =
325+
for {
326+
b <- barrier
327+
_ <- b.complete.when_(count <= 0)
328+
} yield new CountDownLatch(count, b)
329+
323330
}
324331

332+
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
333+
325334
/** Pure asynchronous callback.
326335
*
327336
* You can think of this as being similar to using `Future` - you can use it in for-comprehensions the same way -

0 commit comments

Comments
 (0)