effect-io-ai

Package: effect
Module: FiberMap

FiberMap.runtime

Captures the current runtime and returns a function for forking effects into an existing FiberMap.

Details

Each call stores the forked fiber under the supplied key. If that key already has a fiber, the previous fiber is interrupted unless onlyIfMissing is set.

Example (Capturing a runtime)

import { Context, Effect, FiberMap } from "effect"

interface Users {
  readonly _: unique symbol
}
const Users = Context.Service<Users, {
  getAll: Effect.Effect<Array<unknown>>
}>("Users")

Effect.gen(function*() {
  const map = yield* FiberMap.make<string>()
  const run = yield* FiberMap.runtime(map)<Users>()

  // run some effects and add the fibers to the map
  run("effect-a", Effect.andThen(Users, (_) => _.getAll))
  run("effect-b", Effect.andThen(Users, (_) => _.getAll))
}).pipe(
  Effect.scoped // The fibers will be interrupted when the scope is closed
)

Signature

declare const runtime: <K, A, E>(self: FiberMap<K, A, E>) => <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; }) | undefined) => Fiber.Fiber<XA, XE>), never, R>

Source

Since v2.0.0