import * as Deferred from "./Deferred.ts"; import * as Effect from "./Effect.ts"; import * as Fiber from "./Fiber.ts"; import type * as Inspectable from "./Inspectable.ts"; import * as MutableHashMap from "./MutableHashMap.ts"; import * as Option from "./Option.ts"; import type { Pipeable } from "./Pipeable.ts"; import type * as Scope from "./Scope.ts"; declare const TypeId = "~effect/FiberMap"; /** * A FiberMap is a collection of fibers, indexed by a key. When the associated * Scope is closed, all fibers in the map will be interrupted. Fibers are * automatically removed from the map when they complete. * * **Example** (Managing fibers in a map) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * // Create a FiberMap with string keys * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add some fibers to the map * yield* FiberMap.run(map, "task1", Effect.never) * yield* FiberMap.run(map, "task2", Effect.never) * * // Get the size of the map * return yield* FiberMap.size(map) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 2 * ``` * * @stability stable * @category models * @since 2.0.0 */ export interface FiberMap extends Pipeable, Inspectable.Inspectable, Iterable<[K, Fiber.Fiber]> { readonly [TypeId]: typeof TypeId; readonly deferred: Deferred.Deferred; state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap>; /** Every fiber the map still owns: the keyed ones plus replaced fibers that are still interrupting. */ readonly fibers: Set>; } | { readonly _tag: "Closed"; }; } /** * Returns `true` if a value is a `FiberMap`. * * **Details** * * This is a type guard that checks for the `FiberMap` runtime marker. * * **Example** (Checking if a value is a FiberMap) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * return [FiberMap.isFiberMap(map), FiberMap.isFiberMap({}), FiberMap.isFiberMap(null)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false, false] * ``` * * @stability stable * @category guards * @since 2.0.0 */ export declare const isFiberMap: (u: unknown) => u is FiberMap; /** * 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) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // 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.yieldNow * return yield* FiberMap.size(map) * }).pipe( * Effect.scoped // The fibers will be interrupted when the scope is closed * ) * * const actual = await Effect.runPromise(program) * actual // => 2 * ``` * * @stability stable * @category constructors * @since 2.0.0 */ export declare const make: () => Effect.Effect, never, Scope.Scope>; /** * Creates a scoped run function that forks effects into a new `FiberMap`. * * **Details** * * Each call stores the forked fiber under the supplied key and returns that * fiber. If the key already has a fiber, the previous fiber is interrupted * unless `onlyIfMissing` is set. All managed fibers are interrupted when the * map's scope closes. * * **Example** (Creating a scoped runtime) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const run = yield* FiberMap.makeRuntime() * * // Run effects and get back fibers * const fiber1 = run("task1", Effect.succeed("Hello")) * const fiber2 = run("task2", Effect.succeed("World")) * * // Join the fibers to get their successful values * return [yield* Fiber.join(fiber1), yield* Fiber.join(fiber2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["Hello", "World"] * ``` * * @stability stable * @category constructors * @since 2.0.0 */ export declare const makeRuntime: () => Effect.Effect<(key: K, effect: Effect.Effect, options?: (Effect.RunOptions & { readonly onlyIfMissing?: boolean | undefined; }) | undefined) => Fiber.Fiber, never, Scope.Scope | R>; /** * Creates a scoped run function that forks effects into a new `FiberMap` and * returns a `Promise` for each effect result. * * **When to use** * * Use when keyed fibers must be managed in a scoped map while exposing their * results through Promise-based APIs. * * **Details** * * Each call stores the fiber under the supplied key, interrupting any previous * fiber for that key unless `onlyIfMissing` is set. The returned Promise * resolves with the effect's success value or rejects with the squashed failure * cause. * * **Example** (Creating a promise runtime) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const run = yield* FiberMap.makeRuntimePromise() * * // Run effects and get back promises * const promise1 = run("task1", Effect.succeed("Hello")) * const promise2 = run("task2", Effect.succeed("World")) * * // Convert to Effect and await * return [yield* Effect.promise(() => promise1), yield* Effect.promise(() => promise2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["Hello", "World"] * ``` * * @stability stable * @category constructors * @since 3.13.0 */ export declare const makeRuntimePromise: () => Effect.Effect<(key: K, effect: Effect.Effect, options?: (Effect.RunOptions & { readonly onlyIfMissing?: boolean | undefined; }) | undefined) => Promise, never, Scope.Scope | R>; /** * Adds a fiber to the `FiberMap` under a key using a synchronous, unsafe * mutation. * * **When to use** * * Use when an existing forked fiber must be installed under a key immediately * and synchronous interruption of the replaced fiber is acceptable. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, that previous fiber is interrupted unless `onlyIfMissing` is set; * in that case a different new fiber is interrupted and the existing entry is * kept, while re-registering the existing fiber is a no-op. * * **Example** (Adding a fiber unsafely) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Create a fiber and add it to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * FiberMap.setUnsafe(map, "greeting", fiber) * * yield* Deferred.succeed(deferred, "Hello") * * // Join the fiber to get its successful value * return yield* Fiber.join(fiber) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => "Hello" * ``` * * @stability stable * @category combinators * @since 4.0.0 */ export declare const setUnsafe: { /** * Adds a fiber to the `FiberMap` under a key using a synchronous, unsafe * mutation. * * **When to use** * * Use when an existing forked fiber must be installed under a key immediately * and synchronous interruption of the replaced fiber is acceptable. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, that previous fiber is interrupted unless `onlyIfMissing` is set; * in that case a different new fiber is interrupted and the existing entry is * kept, while re-registering the existing fiber is a no-op. * * **Example** (Adding a fiber unsafely) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Create a fiber and add it to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * FiberMap.setUnsafe(map, "greeting", fiber) * * yield* Deferred.succeed(deferred, "Hello") * * // Join the fiber to get its successful value * return yield* Fiber.join(fiber) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => "Hello" * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (key: K, fiber: Fiber.Fiber, options?: { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; } | undefined): (self: FiberMap) => void; /** * Adds a fiber to the `FiberMap` under a key using a synchronous, unsafe * mutation. * * **When to use** * * Use when an existing forked fiber must be installed under a key immediately * and synchronous interruption of the replaced fiber is acceptable. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, that previous fiber is interrupted unless `onlyIfMissing` is set; * in that case a different new fiber is interrupted and the existing entry is * kept, while re-registering the existing fiber is a no-op. * * **Example** (Adding a fiber unsafely) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Create a fiber and add it to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * FiberMap.setUnsafe(map, "greeting", fiber) * * yield* Deferred.succeed(deferred, "Hello") * * // Join the fiber to get its successful value * return yield* Fiber.join(fiber) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => "Hello" * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (self: FiberMap, key: K, fiber: Fiber.Fiber, options?: { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; } | undefined): void; }; /** * Adds a fiber to the `FiberMap` under a key. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, that previous fiber is interrupted unless `onlyIfMissing` is set; * in that case a different new fiber is interrupted and the existing entry is * kept, while re-registering the existing fiber is a no-op. * * This is the Effect-wrapped version of `setUnsafe`. * * **Example** (Adding a fiber) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Create a fiber and add it to the map using Effect * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * yield* FiberMap.set(map, "greeting", fiber) * * yield* Deferred.succeed(deferred, "Hello") * * // Join the fiber to get its successful value * return yield* Fiber.join(fiber) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => "Hello" * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const set: { /** * Adds a fiber to the `FiberMap` under a key. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, that previous fiber is interrupted unless `onlyIfMissing` is set; * in that case a different new fiber is interrupted and the existing entry is * kept, while re-registering the existing fiber is a no-op. * * This is the Effect-wrapped version of `setUnsafe`. * * **Example** (Adding a fiber) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Create a fiber and add it to the map using Effect * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * yield* FiberMap.set(map, "greeting", fiber) * * yield* Deferred.succeed(deferred, "Hello") * * // Join the fiber to get its successful value * return yield* Fiber.join(fiber) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => "Hello" * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (key: K, fiber: Fiber.Fiber, options?: { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; } | undefined): (self: FiberMap) => Effect.Effect; /** * Adds a fiber to the `FiberMap` under a key. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, that previous fiber is interrupted unless `onlyIfMissing` is set; * in that case a different new fiber is interrupted and the existing entry is * kept, while re-registering the existing fiber is a no-op. * * This is the Effect-wrapped version of `setUnsafe`. * * **Example** (Adding a fiber) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Create a fiber and add it to the map using Effect * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * yield* FiberMap.set(map, "greeting", fiber) * * yield* Deferred.succeed(deferred, "Hello") * * // Join the fiber to get its successful value * return yield* Fiber.join(fiber) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => "Hello" * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberMap, key: K, fiber: Fiber.Fiber, options?: { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; } | undefined): Effect.Effect; }; /** * Retrieves a fiber from the FiberMap synchronously. * * **When to use** * * Use when synchronous keyed lookup of a fiber in a `FiberMap` is needed and an * `Option` result is enough outside the Effect workflow. * * **Example** (Retrieving a fiber unsafely) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap, Option } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Add a fiber to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * FiberMap.setUnsafe(map, "greeting", fiber) * * // Retrieve the fiber * const retrieved = FiberMap.getUnsafe(map, "greeting") * yield* Deferred.succeed(deferred, "Hello") * const result = yield* Fiber.join(fiber) * return Option.map(retrieved, () => result) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => Option.some("Hello") * ``` * * @stability stable * @category combinators * @since 4.0.0 */ export declare const getUnsafe: { /** * Retrieves a fiber from the FiberMap synchronously. * * **When to use** * * Use when synchronous keyed lookup of a fiber in a `FiberMap` is needed and an * `Option` result is enough outside the Effect workflow. * * **Example** (Retrieving a fiber unsafely) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap, Option } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Add a fiber to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * FiberMap.setUnsafe(map, "greeting", fiber) * * // Retrieve the fiber * const retrieved = FiberMap.getUnsafe(map, "greeting") * yield* Deferred.succeed(deferred, "Hello") * const result = yield* Fiber.join(fiber) * return Option.map(retrieved, () => result) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => Option.some("Hello") * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (key: K): (self: FiberMap) => Option.Option>; /** * Retrieves a fiber from the FiberMap synchronously. * * **When to use** * * Use when synchronous keyed lookup of a fiber in a `FiberMap` is needed and an * `Option` result is enough outside the Effect workflow. * * **Example** (Retrieving a fiber unsafely) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap, Option } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Add a fiber to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * FiberMap.setUnsafe(map, "greeting", fiber) * * // Retrieve the fiber * const retrieved = FiberMap.getUnsafe(map, "greeting") * yield* Deferred.succeed(deferred, "Hello") * const result = yield* Fiber.join(fiber) * return Option.map(retrieved, () => result) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => Option.some("Hello") * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (self: FiberMap, key: K): Option.Option>; }; /** * Retrieves a fiber from the FiberMap effectfully. * * **Details** * * Returns an `Option` wrapped in `Effect`. * * **Example** (Retrieving a fiber) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap, Option } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Add a fiber to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * yield* FiberMap.set(map, "greeting", fiber) * * // Retrieve the fiber with error handling * const retrieved = yield* FiberMap.get(map, "greeting") * yield* Deferred.succeed(deferred, "Hello") * const result = yield* Fiber.join(fiber) * return Option.map(retrieved, () => result) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => Option.some("Hello") * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const get: { /** * Retrieves a fiber from the FiberMap effectfully. * * **Details** * * Returns an `Option` wrapped in `Effect`. * * **Example** (Retrieving a fiber) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap, Option } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Add a fiber to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * yield* FiberMap.set(map, "greeting", fiber) * * // Retrieve the fiber with error handling * const retrieved = yield* FiberMap.get(map, "greeting") * yield* Deferred.succeed(deferred, "Hello") * const result = yield* Fiber.join(fiber) * return Option.map(retrieved, () => result) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => Option.some("Hello") * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (key: K): (self: FiberMap) => Effect.Effect>>; /** * Retrieves a fiber from the FiberMap effectfully. * * **Details** * * Returns an `Option` wrapped in `Effect`. * * **Example** (Retrieving a fiber) * * ```ts import.meta.vitest * import { Deferred, Effect, Fiber, FiberMap, Option } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const deferred = yield* Deferred.make() * * // Add a fiber to the map * const fiber = yield* Effect.forkChild(Deferred.await(deferred)) * yield* FiberMap.set(map, "greeting", fiber) * * // Retrieve the fiber with error handling * const retrieved = yield* FiberMap.get(map, "greeting") * yield* Deferred.succeed(deferred, "Hello") * const result = yield* Fiber.join(fiber) * return Option.map(retrieved, () => result) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => Option.some("Hello") * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberMap, key: K): Effect.Effect>>; }; /** * Checks whether a key exists in the FiberMap. * * **Example** (Checking if a key exists unsafely) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add a fiber to the map * yield* FiberMap.run(map, "task1", Effect.never) * * // Check if keys exist * return [FiberMap.hasUnsafe(map, "task1"), FiberMap.hasUnsafe(map, "task2")] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category combinators * @since 4.0.0 */ export declare const hasUnsafe: { /** * Checks whether a key exists in the FiberMap. * * **Example** (Checking if a key exists unsafely) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add a fiber to the map * yield* FiberMap.run(map, "task1", Effect.never) * * // Check if keys exist * return [FiberMap.hasUnsafe(map, "task1"), FiberMap.hasUnsafe(map, "task2")] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (key: K): (self: FiberMap) => boolean; /** * Checks whether a key exists in the FiberMap. * * **Example** (Checking if a key exists unsafely) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add a fiber to the map * yield* FiberMap.run(map, "task1", Effect.never) * * // Check if keys exist * return [FiberMap.hasUnsafe(map, "task1"), FiberMap.hasUnsafe(map, "task2")] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (self: FiberMap, key: K): boolean; }; /** * Checks whether a key exists in the FiberMap. * This is the Effect-wrapped version of `hasUnsafe`. * * **Example** (Checking if a key exists) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add a fiber to the map * yield* FiberMap.run(map, "task1", Effect.never) * * // Check if keys exist using Effect * return [yield* FiberMap.has(map, "task1"), yield* FiberMap.has(map, "task2")] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const has: { /** * Checks whether a key exists in the FiberMap. * This is the Effect-wrapped version of `hasUnsafe`. * * **Example** (Checking if a key exists) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add a fiber to the map * yield* FiberMap.run(map, "task1", Effect.never) * * // Check if keys exist using Effect * return [yield* FiberMap.has(map, "task1"), yield* FiberMap.has(map, "task2")] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (key: K): (self: FiberMap) => Effect.Effect; /** * Checks whether a key exists in the FiberMap. * This is the Effect-wrapped version of `hasUnsafe`. * * **Example** (Checking if a key exists) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add a fiber to the map * yield* FiberMap.run(map, "task1", Effect.never) * * // Check if keys exist using Effect * return [yield* FiberMap.has(map, "task1"), yield* FiberMap.has(map, "task2")] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberMap, key: K): Effect.Effect; }; /** * Removes a fiber from the FiberMap, interrupting it if it exists. * * **Example** (Removing a fiber) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add some fibers to the map * yield* FiberMap.run(map, "task1", Effect.never) * yield* FiberMap.run(map, "task2", Effect.never) * * const sizeBefore = yield* FiberMap.size(map) * * // Remove a specific fiber (this will interrupt it) * yield* FiberMap.remove(map, "task1") * * return [sizeBefore, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [2, 1] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const remove: { /** * Removes a fiber from the FiberMap, interrupting it if it exists. * * **Example** (Removing a fiber) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add some fibers to the map * yield* FiberMap.run(map, "task1", Effect.never) * yield* FiberMap.run(map, "task2", Effect.never) * * const sizeBefore = yield* FiberMap.size(map) * * // Remove a specific fiber (this will interrupt it) * yield* FiberMap.remove(map, "task1") * * return [sizeBefore, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [2, 1] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (key: K): (self: FiberMap) => Effect.Effect; /** * Removes a fiber from the FiberMap, interrupting it if it exists. * * **Example** (Removing a fiber) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add some fibers to the map * yield* FiberMap.run(map, "task1", Effect.never) * yield* FiberMap.run(map, "task2", Effect.never) * * const sizeBefore = yield* FiberMap.size(map) * * // Remove a specific fiber (this will interrupt it) * yield* FiberMap.remove(map, "task1") * * return [sizeBefore, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [2, 1] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberMap, key: K): Effect.Effect; }; /** * Removes all fibers from the FiberMap, interrupting them. * * **Example** (Clearing all fibers) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Add some fibers to the map * yield* FiberMap.run(map, "task1", Effect.never) * yield* FiberMap.run(map, "task2", Effect.never) * yield* FiberMap.run(map, "task3", Effect.never) * * const sizeBefore = yield* FiberMap.size(map) * * // Clear all fibers (this will interrupt all of them) * yield* FiberMap.clear(map) * * return [sizeBefore, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [3, 0] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const clear: (self: FiberMap) => Effect.Effect; /** * Forks an Effect and stores the resulting fiber in the `FiberMap` under a key. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, the previous fiber is interrupted unless `onlyIfMissing` is set. * Set `startImmediately: false` to defer startup. By default, the effect starts * immediately. * * **Example** (Forking effects into a map) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Run effects and add the fibers to the map * const fiber1 = yield* FiberMap.run(map, "task1", Effect.succeed("Hello")) * const fiber2 = yield* FiberMap.run(map, "task2", Effect.succeed("World")) * * // Join the fibers to get their successful values * const result1 = yield* Fiber.join(fiber1) * const result2 = yield* Fiber.join(fiber2) * return [result1, result2, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["Hello", "World", 0] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const run: { /** * Forks an Effect and stores the resulting fiber in the `FiberMap` under a key. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, the previous fiber is interrupted unless `onlyIfMissing` is set. * Set `startImmediately: false` to defer startup. By default, the effect starts * immediately. * * **Example** (Forking effects into a map) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Run effects and add the fibers to the map * const fiber1 = yield* FiberMap.run(map, "task1", Effect.succeed("Hello")) * const fiber2 = yield* FiberMap.run(map, "task2", Effect.succeed("World")) * * // Join the fibers to get their successful values * const result1 = yield* Fiber.join(fiber1) * const result2 = yield* Fiber.join(fiber2) * return [result1, result2, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["Hello", "World", 0] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberMap, key: K, options?: { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; readonly startImmediately?: boolean | undefined; } | undefined): (effect: Effect.Effect) => Effect.Effect, never, R>; /** * Forks an Effect and stores the resulting fiber in the `FiberMap` under a key. * * **Details** * * When the fiber completes, it is removed from the map. If the key already has * a fiber, the previous fiber is interrupted unless `onlyIfMissing` is set. * Set `startImmediately: false` to defer startup. By default, the effect starts * immediately. * * **Example** (Forking effects into a map) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * // Run effects and add the fibers to the map * const fiber1 = yield* FiberMap.run(map, "task1", Effect.succeed("Hello")) * const fiber2 = yield* FiberMap.run(map, "task2", Effect.succeed("World")) * * // Join the fibers to get their successful values * const result1 = yield* Fiber.join(fiber1) * const result2 = yield* Fiber.join(fiber2) * return [result1, result2, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["Hello", "World", 0] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberMap, key: K, effect: Effect.Effect, options?: { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; readonly startImmediately?: boolean | undefined; } | undefined): Effect.Effect, never, R>; }; /** * 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) * * ```ts import.meta.vitest * import { Context, Effect, Fiber, FiberMap } from "effect" * * class Users extends Context.Service> * }>()("Users") {} * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const run = yield* FiberMap.runtime(map)() * * // run some effects and add the fibers to the map * const fiberA = run("effect-a", Effect.andThen(Users, (_) => _.getAll)) * const fiberB = run("effect-b", Effect.andThen(Users, (_) => _.getAll)) * return [(yield* Fiber.join(fiberA)).length, (yield* Fiber.join(fiberB)).length] * }).pipe( * Effect.scoped // The fibers will be interrupted when the scope is closed * ) * * const actual = await Effect.runPromise(Effect.provideService(program, Users, { * getAll: Effect.succeed([]) * })) * actual // => [0, 0] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const runtime: (self: FiberMap) => () => Effect.Effect<(key: K, effect: Effect.Effect, options?: (Effect.RunOptions & { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; }) | undefined) => Fiber.Fiber, never, R>; /** * Captures the current runtime and returns a function for running effects in * an existing `FiberMap` as Promises. * * **Details** * * Each call stores the forked fiber under the supplied key, interrupting any * previous fiber for that key unless `onlyIfMissing` is set. The Promise * resolves with the effect's success value or rejects with the squashed failure * cause. * * **Example** (Running effects as promises) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * const runPromise = yield* FiberMap.runtimePromise(map)() * * // Create promises that will be backed by fibers in the map * const promise1 = runPromise("task1", Effect.succeed("Hello")) * const promise2 = runPromise("task2", Effect.succeed("World")) * * // Convert promises back to Effects and await * return [yield* Effect.promise(() => promise1), yield* Effect.promise(() => promise2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["Hello", "World"] * ``` * * @stability stable * @category combinators * @since 3.13.0 */ export declare const runtimePromise: (self: FiberMap) => () => Effect.Effect<(key: K, effect: Effect.Effect, options?: (Effect.RunOptions & { readonly onlyIfMissing?: boolean | undefined; readonly propagateInterruption?: boolean | undefined; }) | undefined) => Promise, never, R>; /** * Gets the number of fibers currently in the FiberMap. * * **Example** (Checking the map size) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * const sizeBefore = yield* FiberMap.size(map) * * // Add some fibers * yield* FiberMap.run(map, "task1", Effect.never) * yield* FiberMap.run(map, "task2", Effect.never) * * return [sizeBefore, yield* FiberMap.size(map)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [0, 2] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const size: (self: FiberMap) => Effect.Effect; /** * Waits for the `FiberMap` to fail or close. * * **Details** * * The returned Effect fails with the first managed fiber failure that is not * ignored by the map's interruption rules. Normal successful completion * removes fibers from the map; use `awaitEmpty` to wait until the map has no * fibers. * * **Example** (Joining failing fibers) * * ```ts import.meta.vitest * import { Effect, Exit, FiberMap } from "effect" * * const program = 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) * }) * * const actual = await Effect.runPromise(Effect.exit(Effect.scoped(program))) * actual // => Exit.fail("error") * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const join: (self: FiberMap) => Effect.Effect; /** * Waits for the FiberMap to be empty. * This will wait for all currently running fibers to complete. * * **Example** (Waiting for an empty map) * * ```ts import.meta.vitest * import { Effect, FiberMap } from "effect" * * const program = Effect.gen(function*() { * const map = yield* FiberMap.make() * * yield* FiberMap.run(map, "task1", Effect.yieldNow) * yield* FiberMap.run(map, "task2", Effect.yieldNow) * * // Wait for the map to be empty * yield* FiberMap.awaitEmpty(map) * * return yield* FiberMap.size(map) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 0 * ``` * * @stability stable * @category combinators * @since 3.13.0 */ export declare const awaitEmpty: (self: FiberMap) => Effect.Effect; export {}; //# sourceMappingURL=FiberMap.d.ts.map