From 03f5be6728b780e97e5ecc8b1ea660a1c250257b Mon Sep 17 00:00:00 2001 From: "Diego E. Alonso" Date: Tue, 11 Jul 2023 20:14:17 +0100 Subject: [PATCH] Sub timeout - remove unused F wrappers. 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. --- tyrian/js/src/main/scala/tyrian/Sub.scala | 28 +++++++---------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/tyrian/js/src/main/scala/tyrian/Sub.scala b/tyrian/js/src/main/scala/tyrian/Sub.scala index eea0b618..2adda388 100644 --- a/tyrian/js/src/main/scala/tyrian/Sub.scala +++ b/tyrian/js/src/main/scala/tyrian/Sub.scala @@ -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] =