Skip to main content Link Search Menu Expand Document (external link)

FiberMap.ts overview

Since v2.0.0


Exports Grouped by Category


utils

FiberMap (interface)

Signature

export interface FiberMap<in out K, out A = unknown, out E = unknown>
  extends Pipeable,
    Inspectable.Inspectable,
    Iterable<[K, Fiber.RuntimeFiber<A, E>]> {
  readonly [TypeId]: TypeId
  readonly deferred: Deferred.Deferred<void, unknown>
  /** @internal */
  state:
    | {
        readonly _tag: "Open"
        readonly backing: MutableHashMap.MutableHashMap<K, Fiber.RuntimeFiber<A, E>>
      }
    | {
        readonly _tag: "Closed"
      }
}

Source

Since v2.0.0

TypeId

Signature

declare const TypeId: unique symbol

Source

Since v2.0.0

TypeId (type alias)

Signature

type TypeId = typeof TypeId

Source

Since v2.0.0

awaitEmpty

Wait for the FiberMap to be empty.

Signature

declare const awaitEmpty: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<void, E>

Source

Since v3.13.0

clear

Signature

declare const clear: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<void>

Source

Since v2.0.0

get

Retrieve a fiber from the FiberMap.

Signature

declare const get: {
  <K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Effect.Effect<Fiber.RuntimeFiber<A, E>, NoSuchElementException>
  <K, A, E>(self: FiberMap<K, A, E>, key: K): Effect.Effect<Fiber.RuntimeFiber<A, E>, NoSuchElementException>
}

Source

Since v2.0.0

has

Check if a key exists in the FiberMap.

Signature

declare const has: {
  <K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Effect.Effect<boolean>
  <K, A, E>(self: FiberMap<K, A, E>, key: K): Effect.Effect<boolean>
}

Source

Since v2.0.0

isFiberMap

Signature

declare const isFiberMap: (u: unknown) => u is FiberMap<unknown>

Source

Since v2.0.0

join

Join all fibers in the FiberMap. If any of the Fiber’s in the map terminate with a failure, the returned Effect will terminate with the first failure that occurred.

Example

import { Effect, FiberMap } from "effect"

Effect.gen(function* (_) {
  const map = yield* _(FiberMap.make())
  yield* _(FiberMap.set(map, "a", Effect.runFork(Effect.fail("error"))))

  // parent fiber will fail with "error"
  yield* _(FiberMap.join(map))
})

Signature

declare const join: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<void, E>

Source

Since v2.0.0

make

A FiberMap can be used to store a collection of fibers, indexed by some key. When the associated Scope is closed, all fibers in the map will be interrupted.

You can add fibers to the map using FiberMap.set or FiberMap.run, and the fibers will be automatically removed from the FiberMap when they complete.

Example

import { Effect, FiberMap } from "effect"

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

  // run some effects and add the fibers to the map
  yield* FiberMap.run(map, "fiber a", Effect.never)
  yield* FiberMap.run(map, "fiber b", Effect.never)

  yield* Effect.sleep(1000)
}).pipe(
  Effect.scoped // The fibers will be interrupted when the scope is closed
)

Signature

declare const make: <K, A = unknown, E = unknown>() => Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>

Source

Since v2.0.0

makeRuntime

Create an Effect run function that is backed by a FiberMap.

Signature

declare const makeRuntime: <R, K, E = unknown, A = unknown>() => Effect.Effect<
  <XE extends E, XA extends A>(
    key: K,
    effect: Effect.Effect<XA, XE, R>,
    options?: (Runtime.RunForkOptions & { readonly onlyIfMissing?: boolean | undefined }) | undefined
  ) => Fiber.RuntimeFiber<XA, XE>,
  never,
  Scope.Scope | R
>

Source

Since v2.0.0

makeRuntimePromise

Create an Effect run function that is backed by a FiberMap.

Signature

declare const makeRuntimePromise: <R, K, A = unknown, E = unknown>() => Effect.Effect<
  <XE extends E, XA extends A>(
    key: K,
    effect: Effect.Effect<XA, XE, R>,
    options?: (Runtime.RunForkOptions & { readonly onlyIfMissing?: boolean | undefined }) | undefined
  ) => Promise<XA>,
  never,
  Scope.Scope | R
>

Source

Since v3.13.0

remove

Remove a fiber from the FiberMap, interrupting it if it exists.

Signature

declare const remove: {
  <K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Effect.Effect<void>
  <K, A, E>(self: FiberMap<K, A, E>, key: K): Effect.Effect<void>
}

Source

Since v2.0.0

run

Run an Effect and add the forked fiber to the FiberMap. When the fiber completes, it will be removed from the FiberMap.

Signature

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

Source

Since v2.0.0

runtime

Capture a Runtime and use it to fork Effect’s, adding the forked fibers to the FiberMap.

Example

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

interface Users {
  readonly _: unique symbol
}
const Users = Context.GenericTag<
  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?:
      | (Runtime.RunForkOptions & {
          readonly onlyIfMissing?: boolean | undefined
          readonly propagateInterruption?: boolean | undefined
        })
      | undefined
  ) => Fiber.RuntimeFiber<XA, XE>,
  never,
  R
>

Source

Since v2.0.0

runtimePromise

Capture a Runtime and use it to fork Effect’s, adding the forked fibers to the FiberMap.

Signature

declare const runtimePromise: <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?:
      | (Runtime.RunForkOptions & {
          readonly onlyIfMissing?: boolean | undefined
          readonly propagateInterruption?: boolean | undefined
        })
      | undefined
  ) => Promise<XA>,
  never,
  R
>

Source

Since v3.13.0

set

Add a fiber to the FiberMap. When the fiber completes, it will be removed from the FiberMap. If the key already exists in the FiberMap, the previous fiber will be interrupted.

Signature

declare const set: {
  <K, A, E, XE extends E, XA extends A>(
    key: K,
    fiber: Fiber.RuntimeFiber<XA, XE>,
    options?:
      | { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined }
      | undefined
  ): (self: FiberMap<K, A, E>) => Effect.Effect<void>
  <K, A, E, XE extends E, XA extends A>(
    self: FiberMap<K, A, E>,
    key: K,
    fiber: Fiber.RuntimeFiber<XA, XE>,
    options?:
      | { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined }
      | undefined
  ): Effect.Effect<void>
}

Source

Since v2.0.0

size

Signature

declare const size: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<number>

Source

Since v2.0.0

unsafeGet

Retrieve a fiber from the FiberMap.

Signature

declare const unsafeGet: {
  <K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Option.Option<Fiber.RuntimeFiber<A, E>>
  <K, A, E>(self: FiberMap<K, A, E>, key: K): Option.Option<Fiber.RuntimeFiber<A, E>>
}

Source

Since v2.0.0

unsafeHas

Check if a key exists in the FiberMap.

Signature

declare const unsafeHas: {
  <K>(key: K): <A, E>(self: FiberMap<K, A, E>) => boolean
  <K, A, E>(self: FiberMap<K, A, E>, key: K): boolean
}

Source

Since v2.0.0

unsafeSet

Add a fiber to the FiberMap. When the fiber completes, it will be removed from the FiberMap. If the key already exists in the FiberMap, the previous fiber will be interrupted.

Signature

declare const unsafeSet: {
  <K, A, E, XE extends E, XA extends A>(
    key: K,
    fiber: Fiber.RuntimeFiber<XA, XE>,
    options?:
      | {
          readonly interruptAs?: FiberId.FiberId | undefined
          readonly onlyIfMissing?: boolean | undefined
          readonly propagateInterruption?: boolean | undefined
        }
      | undefined
  ): (self: FiberMap<K, A, E>) => void
  <K, A, E, XE extends E, XA extends A>(
    self: FiberMap<K, A, E>,
    key: K,
    fiber: Fiber.RuntimeFiber<XA, XE>,
    options?:
      | {
          readonly interruptAs?: FiberId.FiberId | undefined
          readonly onlyIfMissing?: boolean | undefined
          readonly propagateInterruption?: boolean | undefined
        }
      | undefined
  ): void
}

Source

Since v2.0.0