arrow-fx-coroutines / arrow.fx.coroutines / ForkScoped

ForkScoped

suspend fun <A> ~~ForkScoped~~(ctx: CoroutineContext = Dispatchers.Default, interruptWhen: suspend () -> Unit, f: suspend () -> A): Fiber<A> Deprecated: Use Deferred with KotlinX Coroutines Structured Concurrency

Launches a new suspendable cancellable coroutine within a Fiber. It does so by connecting the created Fiber’s cancellation to the provided interruptWhen. If the interruptWhen signal gets triggered, then this Fiber will get cancelled.

You can still cancel the Fiber independent from the interruptWhen token; whichever one comes first cancels the Fiber.

This function is meant to integrate with 3rd party cancellation system such as Android.

import arrow.fx.coroutines.*

tailrec suspend fun parallelProcess(): Unit {
  println(System.currentTimeMillis())
  sleep(1.seconds)
  parallelProcess()
}

suspend fun main(): Unit {
  val switch = Promise<Unit>()
  val switcher = suspend {
    sleep(5.seconds)
    switch.complete(Unit)
  }

  ::parallelProcess.forkScoped(interruptWhen = switch::get)
  switcher.forkConnected()
}

Do you like Arrow?

Arrow Org
<