Hyperlinkv0.8.0-beta.28

FiberMap

FiberMap.makeconsteffect/FiberMap.ts:155
<K, A = unknown, E = unknown>(): Effect.Effect<
  FiberMap<K, A, E>,
  never,
  Scope.Scope
>

Creates a scoped FiberMap for storing fibers by key.

Details

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 (Creating a scoped FiberMap)

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
)
constructors
Source effect/FiberMap.ts:15518 lines
export const make = <K, A = unknown, E = unknown>(): Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope> =>
  Effect.acquireRelease(
    Effect.sync(() =>
      makeUnsafe<K, A, E>(
        MutableHashMap.empty(),
        Deferred.makeUnsafe()
      )
    ),
    (map) =>
      Effect.suspend(() => {
        const state = map.state
        if (state._tag === "Closed") return Effect.void
        map.state = { _tag: "Closed" }
        return Fiber.interruptAll(MutableHashMap.values(state.backing)).pipe(
          Deferred.into(map.deferred)
        )
      })
  )
Referenced by 2 symbols