Skip to content

Commit 5b322f7

Browse files
committed
Add .dispatch to {,Async}Callback
Closes #948
1 parent ad47a42 commit 5b322f7

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

callback/src/main/scala-2/japgolly/scalajs/react/callback/AsyncCallback.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,13 @@ final class AsyncCallback[+A] private[AsyncCallback] (val underlyingRepr: AsyncC
10921092
def fork_ : Callback =
10931093
delayMs(1).toCallback
10941094

1095+
/** Runs this async computation in the background.
1096+
*
1097+
* Unlike [[fork_]] this returns an `AsyncCallback[Unit]` instead of a `Callback`.
1098+
*/
1099+
def dispatch: AsyncCallback[Unit] =
1100+
delayMs(1).void
1101+
10951102
/** Record the duration of this callback's execution. */
10961103
def withDuration[B](f: (A, FiniteDuration) => AsyncCallback[B]): AsyncCallback[B] = {
10971104
val nowMS: AsyncCallback[Long] = CallbackTo.currentTimeMillis.asAsyncCallback

callback/src/main/scala-2/japgolly/scalajs/react/callback/CallbackTo.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ final class CallbackTo[+A] private[react] (private[CallbackTo] val trampoline: T
695695
}
696696
}
697697

698+
/** Runs this computation in the background. */
699+
def dispatch: Callback =
700+
asAsyncCallback.fork_
701+
698702
def withFilter(f: A => Boolean): CallbackTo[A] =
699703
map[A](a => if (f(a)) a else
700704
// This is what scala.Future does

callback/src/main/scala-3/japgolly/scalajs/react/callback/AsyncCallback.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,13 @@ final class AsyncCallback[+A] private[AsyncCallback] (val underlyingRepr: AsyncC
10961096
inline def fork_ : Callback =
10971097
delayMs(1).toCallback
10981098

1099+
/** Runs this async computation in the background.
1100+
*
1101+
* Unlike [[fork_]] this returns an `AsyncCallback[Unit]` instead of a `Callback`.
1102+
*/
1103+
def dispatch: AsyncCallback[Unit] =
1104+
delayMs(1).void
1105+
10991106
/** Record the duration of this callback's execution. */
11001107
def withDuration[B](f: (A, FiniteDuration) => AsyncCallback[B]): AsyncCallback[B] = {
11011108
val nowMS: AsyncCallback[Long] = CallbackTo.currentTimeMillis.asAsyncCallback

callback/src/main/scala-3/japgolly/scalajs/react/callback/CallbackTo.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,10 @@ final class CallbackTo[+A] /*private[react]*/ (private[CallbackTo] val trampolin
722722
}
723723
}
724724

725+
/** Runs this computation in the background. */
726+
def dispatch: Callback =
727+
asAsyncCallback.fork_
728+
725729
def withFilter(f: A => Boolean): CallbackTo[A] =
726730
map[A](a => if f(a) then a else
727731
// This is what scala.Future does

doc/changelog/2.0.0.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ You can run the script in the Migration section at the bottom of the page to aut
339339
*(e.g. `renderBackend[Backend[X]]` where `X` is a type already in scope)*
340340
* Support `CallbackOption[Unit]` being passed directly to event handling vdom
341341
342-
* `TriStateCheckbox` now accepts an optional `Reusable[TagMod]` in its `Props` that will be applied to the `<input>`
342+
* Additions:
343+
* `TriStateCheckbox` now accepts an optional `Reusable[TagMod]` in its `Props` that will be applied to the `<input>`
344+
* Add `.dispatch` to `Callback` and `AsyncCallback` which schedules (-and-forgets) the callback to be run in the background
343345
344346
* Upgrade deps
345347
* Cats-effect to 3.2.2

0 commit comments

Comments
 (0)