Forking and Racing
A simple typeclass, which main responsibility is to start a process in the background and forget about the result value.
import tofu.env._import tofu.syntax.fire._
// some long-running computationval longComputation: Env[Unit, Unit] = Env.delay(())
// this is now running in the background, all errors will be silently discardedval started: Env[Unit, Unit] = longComputation.fireAndForget
Allows to run two computations concurrently, returning the result of either completed first (or failed first).
import cats.Functorimport cats.syntax.functor._import tofu.Raceimport tofu.syntax.race._
// will result in two computations started in parallel,// then the first one to return will be handled// the other will be canceleddef raceAndPickFirst[F[_]: Race: Functor, A, B](f1: F[A], f2: F[B]): F[String] = { val aOrB: F[Either[A, B]] = f1.race(f2)
aOrB.map { case Left(a) => s"Return A: $a" case Right(b) => s"Return B: $b" }}
Allows starting computation, resulting in Fiber
which can then be joined on or canceled.
import cats.effect.Fiberimport cats.FlatMapimport cats.syntax.flatMap._import tofu.Startimport tofu.syntax.start._
// will result in computation being started// in background and then immediately canceleddef startAndCancel[F[_]: Start: FlatMap, A](f: F[A]): F[Unit] = { val fiber: F[Fiber[F, A]] = f.start fiber.flatMap(_.cancel)}
TBD.