Skip to content

Commit

Permalink
Sub timeout - remove unused F wrappers.
Browse files Browse the repository at this point in the history
The external `F.delay` wrappers was not useful in any way,
since they were being immediately unwraped in the body of
`task`, so they have no effect but cost allocations.

Note that it is the function itself that delays the side
effectful operations on `dom.window` in acquire.
  • Loading branch information
diesalbla authored and davesmith00000 committed Jul 12, 2023
1 parent 0838844 commit 03f5be6
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions tyrian/js/src/main/scala/tyrian/Sub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,16 @@ object Sub:

/** A subscription that produces a `msg` after a `duration`. */
def timeout[F[_]: Sync, Msg](duration: FiniteDuration, msg: Msg, id: String): Sub[F, Msg] =
val acquire: F[(Either[Throwable, Msg] => Unit) => Int] =
Sync[F].delay { (callback: Either[Throwable, Msg] => Unit) =>
dom.window.setTimeout(
Functions.fun0(() => callback(Right(msg))),
duration.toMillis.toDouble
)
}

val release: F[Int => F[Option[F[Unit]]]] =
Sync[F].delay { (handle: Int) =>
Sync[F].delay {
Option(Sync[F].delay(dom.window.clearTimeout(handle)))
}
def task(callback: Either[Throwable, Msg] => Unit): F[Option[F[Unit]]] =
val handle = dom.window.setTimeout(
Functions.fun0(() => callback(Right(msg))),
duration.toMillis.toDouble
)
Sync[F].delay {
Option(Sync[F].delay(dom.window.clearTimeout(handle)))
}

val task =
for {
a <- acquire
r <- release
} yield a andThen r

Observe(id, task)
Observe(id, Sync[F].pure(task))

/** A subscription that produces a `msg` after a `duration`. */
def timeout[F[_]: Sync, Msg](duration: FiniteDuration, msg: Msg): Sub[F, Msg] =
Expand Down

0 comments on commit 03f5be6

Please sign in to comment.