Skip to content

FiberMap

21 exports Added in v2.0.0 Source

Other

awaitEmpty

Added in v3.13.0 Source

Wait for the FiberMap to be empty.

Signature

declare function awaitEmpty<K, A, E>(self: FiberMap<K, A, E>): Effect<void, E>;

clear

Added in v2.0.0 Source

Signature

declare function clear<K, A, E>(self: FiberMap<K, A, E>): Effect<void>;

FiberMap interface

Added in v2.0.0 Source

Signature

interface FiberMap<in out K, out A = unknown, out E = unknown> extends Pipeable, Inspectable, "/home/runner/work/website/website/.effect-source-v3/packages/effect/src/Iterable"<[K, Fiber.RuntimeFiber<A, E>]> {
  readonly [TypeId]: typeof TypeId;
  readonly deferred: Deferred<void, unknown>;
}

get

Added in v2.0.0 Source

Retrieve a fiber from the FiberMap.

Signature

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

has

Added in v2.0.0 Source

Check if a key exists in the FiberMap.

Signature

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

isFiberMap

Added in v2.0.0 Source

Signature

declare function isFiberMap(u: unknown): u is FiberMap<unknown, unknown, unknown>;

join

Added in v2.0.0 Source

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.

Signature

declare function join<K, A, E>(self: FiberMap<K, A, E>): Effect<void, E>;

make

Added in v2.0.0 Source

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.

Signature

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

makeRuntime

Added in v2.0.0 Source

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

Signature

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

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

Signature

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

remove

Added in v2.0.0 Source

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<void>;
  <K, A, E>(self: FiberMap<K, A, E>, key: K): Effect<void>;
};

run

Added in v2.0.0 Source

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;
      readonly propagateInterruption?: boolean;
    },
  ): <R, XE, XA>(effect: Effect<XA, XE, R>) => Effect<RuntimeFiber<XA, XE>, never, R>;
  <K, A, E, R, XE, XA>(
    self: FiberMap<K, A, E>,
    key: K,
    effect: Effect<XA, XE, R>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): Effect<RuntimeFiber<XA, XE>, never, R>;
};

runtime

Added in v2.0.0 Source

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

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;
      readonly propagateInterruption?: boolean;
    },
  ) => Fiber.RuntimeFiber<XA, XE>,
  never,
  R
>;

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
)

runtimePromise

Added in v3.13.0 Source

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

Signature

declare function runtimePromise<K, A, E>(
  self: FiberMap<K, A, E>,
): <R = never>() => Effect<
  <XE, XA>(
    key: K,
    effect: Effect<XA, XE, R>,
    options?: RunForkOptions & {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ) => Promise<XA>,
  never,
  R
>;

set

Added in v2.0.0 Source

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, XA>(
    key: K,
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): (self: FiberMap<K, A, E>) => Effect<void>;
  <K, A, E, XE, XA>(
    self: FiberMap<K, A, E>,
    key: K,
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): Effect<void>;
};

size

Added in v2.0.0 Source

Signature

declare function size<K, A, E>(self: FiberMap<K, A, E>): Effect<number>;

TypeId

Added in v2.0.0 Source

Signature

declare const TypeId: unique symbol;

TypeId type

Added in v2.0.0 Source

Signature

type TypeId = typeof TypeId;

unsafeGet

Added in v2.0.0 Source

Retrieve a fiber from the FiberMap.

Signature

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

unsafeHas

Added in v2.0.0 Source

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;
};

unsafeSet

Added in v2.0.0 Source

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, XA>(
    key: K,
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly interruptAs?: FiberId.FiberId;
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): (self: FiberMap<K, A, E>) => void;
  <K, A, E, XE, XA>(
    self: FiberMap<K, A, E>,
    key: K,
    fiber: RuntimeFiber<XA, XE>,
    options?: {
      readonly interruptAs?: FiberId.FiberId;
      readonly onlyIfMissing?: boolean;
      readonly propagateInterruption?: boolean;
    },
  ): void;
};