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 type { Pipeable } from "./Pipeable.ts"; import type * as Scope from "./Scope.ts"; declare const TypeId = "~effect/FiberSet"; /** * A FiberSet is a collection of fibers that can be managed together. * When the associated Scope is closed, all fibers in the set will be interrupted. * * **Example** (Managing fibers in a set) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * // Add fibers to the set * yield* FiberSet.run(set, Effect.succeed("hello")) * yield* FiberSet.run(set, Effect.succeed("world")) * * // Wait for all fibers to complete * yield* FiberSet.awaitEmpty(set) * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 0 * ``` * * @stability stable * @category models * @since 2.0.0 */ export interface FiberSet extends Pipeable, Inspectable.Inspectable, Iterable> { readonly [TypeId]: typeof TypeId; readonly deferred: Deferred.Deferred; state: { readonly _tag: "Open"; readonly backing: Set>; } | { readonly _tag: "Closed"; }; } /** * Checks whether a value is a FiberSet. * * **Example** (Checking if a value is a FiberSet) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * return [FiberSet.isFiberSet(set), FiberSet.isFiberSet({})] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [true, false] * ``` * * @stability stable * @category guards * @since 2.0.0 */ export declare const isFiberSet: (u: unknown) => u is FiberSet; /** * Creates a scoped `FiberSet` for storing fibers. * * **Details** * * When the associated Scope is closed, all fibers in the set will be * interrupted. You can add fibers to the set using `FiberSet.add` or * `FiberSet.run`, and the fibers will be automatically removed from the * FiberSet when they complete. * * **Example** (Creating a scoped FiberSet) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * // run some effects and add the fibers to the set * yield* FiberSet.run(set, Effect.never) * yield* FiberSet.run(set, Effect.never) * * yield* Effect.yieldNow * return yield* FiberSet.size(set) * }).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 `FiberSet`. * * **Details** * * Each call returns the forked fiber and adds it to the set. Managed fibers are * removed when they complete and are interrupted when the set's scope closes. * * **Example** (Creating a scoped runtime) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const runFork = yield* FiberSet.makeRuntime() * * // Fork effects using the runtime * const fiber1 = runFork(Effect.succeed("hello")) * const fiber2 = runFork(Effect.succeed("world")) * * 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<((effect: Effect.Effect, options?: (Effect.RunOptions & { readonly propagateInterruption?: boolean | undefined; }) | undefined) => Fiber.Fiber), never, Scope.Scope | R>; /** * Creates a scoped run function that forks effects into a new `FiberSet` and * returns a `Promise` for each effect result. * * **When to use** * * Use when many scoped fibers should be tracked as a set while exposing each * result through Promise-based APIs. * * **Details** * * Managed fibers are removed when they complete and are interrupted when the * set's scope closes. Each 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, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const runPromise = yield* FiberSet.makeRuntimePromise() * * // Run effects as promises * const promise1 = runPromise(Effect.succeed("hello")) * const promise2 = runPromise(Effect.succeed("world")) * * 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<((effect: Effect.Effect, options?: (Effect.RunOptions & { readonly propagateInterruption?: boolean | undefined; }) | undefined) => Promise), never, R | Scope.Scope>; /** * Adds an existing fiber to the `FiberSet` using a synchronous, unsafe * mutation. * * **When to use** * * Use when an already forked fiber must be registered immediately and * synchronous interruption on a closed set is acceptable. * * **Details** * * When the fiber completes, it is removed from the set. If the set is already * closed, the supplied fiber is interrupted immediately. Non-interruption * failures are recorded for `FiberSet.join`. * * **Example** (Adding a fiber unsafely) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const fiber = yield* Effect.forkChild(Effect.never) * * // Unsafe add - doesn't return an Effect * FiberSet.addUnsafe(set, fiber) * * // The fiber is now managed by the set * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 1 * ``` * * @stability stable * @category combinators * @since 4.0.0 */ export declare const addUnsafe: { /** * Adds an existing fiber to the `FiberSet` using a synchronous, unsafe * mutation. * * **When to use** * * Use when an already forked fiber must be registered immediately and * synchronous interruption on a closed set is acceptable. * * **Details** * * When the fiber completes, it is removed from the set. If the set is already * closed, the supplied fiber is interrupted immediately. Non-interruption * failures are recorded for `FiberSet.join`. * * **Example** (Adding a fiber unsafely) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const fiber = yield* Effect.forkChild(Effect.never) * * // Unsafe add - doesn't return an Effect * FiberSet.addUnsafe(set, fiber) * * // The fiber is now managed by the set * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 1 * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (fiber: Fiber.Fiber, options?: { readonly propagateInterruption?: boolean | undefined; } | undefined): (self: FiberSet) => void; /** * Adds an existing fiber to the `FiberSet` using a synchronous, unsafe * mutation. * * **When to use** * * Use when an already forked fiber must be registered immediately and * synchronous interruption on a closed set is acceptable. * * **Details** * * When the fiber completes, it is removed from the set. If the set is already * closed, the supplied fiber is interrupted immediately. Non-interruption * failures are recorded for `FiberSet.join`. * * **Example** (Adding a fiber unsafely) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const fiber = yield* Effect.forkChild(Effect.never) * * // Unsafe add - doesn't return an Effect * FiberSet.addUnsafe(set, fiber) * * // The fiber is now managed by the set * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 1 * ``` * * @stability stable * @category combinators * @since 4.0.0 */ (self: FiberSet, fiber: Fiber.Fiber, options?: { readonly propagateInterruption?: boolean | undefined; } | undefined): void; }; /** * Adds a fiber to the FiberSet. When the fiber completes, it will be removed. * * **Example** (Adding a fiber) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const fiber = yield* Effect.forkChild(Effect.never) * * // Add the fiber to the set * yield* FiberSet.add(set, fiber) * * // The fiber is now managed by the set * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 1 * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const add: { /** * Adds a fiber to the FiberSet. When the fiber completes, it will be removed. * * **Example** (Adding a fiber) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const fiber = yield* Effect.forkChild(Effect.never) * * // Add the fiber to the set * yield* FiberSet.add(set, fiber) * * // The fiber is now managed by the set * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 1 * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (fiber: Fiber.Fiber, options?: { readonly propagateInterruption?: boolean | undefined; } | undefined): (self: FiberSet) => Effect.Effect; /** * Adds a fiber to the FiberSet. When the fiber completes, it will be removed. * * **Example** (Adding a fiber) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const fiber = yield* Effect.forkChild(Effect.never) * * // Add the fiber to the set * yield* FiberSet.add(set, fiber) * * // The fiber is now managed by the set * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 1 * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberSet, fiber: Fiber.Fiber, options?: { readonly propagateInterruption?: boolean | undefined; } | undefined): Effect.Effect; }; /** * Interrupts all fibers in the `FiberSet` and clears the set. * * **Example** (Clearing all fibers) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * // Add some fibers * yield* FiberSet.run(set, Effect.never) * yield* FiberSet.run(set, Effect.never) * * const sizeBefore = yield* FiberSet.size(set) * * // Clear all fibers * yield* FiberSet.clear(set) * * return [sizeBefore, yield* FiberSet.size(set)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [2, 0] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const clear: (self: FiberSet) => Effect.Effect; /** * Forks an Effect and adds the fiber to the FiberSet. The fiber is removed * when it completes. Set `startImmediately: false` to defer startup. By default, * the effect starts immediately. * * **Example** (Forking effects into a set) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * // Fork and add to set * const fiber1 = yield* FiberSet.run(set, Effect.succeed("hello")) * const fiber2 = yield* FiberSet.run(set, Effect.succeed("world")) * * // Get results * return [yield* Fiber.join(fiber1), yield* Fiber.join(fiber2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["hello", "world"] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const run: { /** * Forks an Effect and adds the fiber to the FiberSet. The fiber is removed * when it completes. Set `startImmediately: false` to defer startup. By default, * the effect starts immediately. * * **Example** (Forking effects into a set) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * // Fork and add to set * const fiber1 = yield* FiberSet.run(set, Effect.succeed("hello")) * const fiber2 = yield* FiberSet.run(set, Effect.succeed("world")) * * // Get results * return [yield* Fiber.join(fiber1), yield* Fiber.join(fiber2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["hello", "world"] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberSet, options?: { readonly propagateInterruption?: boolean | undefined; readonly startImmediately?: boolean | undefined; } | undefined): (effect: Effect.Effect) => Effect.Effect, never, R>; /** * Forks an Effect and adds the fiber to the FiberSet. The fiber is removed * when it completes. Set `startImmediately: false` to defer startup. By default, * the effect starts immediately. * * **Example** (Forking effects into a set) * * ```ts import.meta.vitest * import { Effect, Fiber, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * // Fork and add to set * const fiber1 = yield* FiberSet.run(set, Effect.succeed("hello")) * const fiber2 = yield* FiberSet.run(set, Effect.succeed("world")) * * // Get results * return [yield* Fiber.join(fiber1), yield* Fiber.join(fiber2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["hello", "world"] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ (self: FiberSet, effect: Effect.Effect, options?: { readonly propagateInterruption?: boolean | undefined; readonly startImmediately?: boolean | undefined; } | undefined): Effect.Effect, never, R>; }; /** * Captures a `Runtime` and uses it to fork effects into the `FiberSet`. * * **Example** (Capturing a runtime) * * ```ts import.meta.vitest * import { Context, Effect, Fiber, FiberSet } from "effect" * * class Users extends Context.Service> * }>()("Users") {} * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const run = yield* FiberSet.runtime(set)() * * // run some effects and add the fibers to the set * const fiber = run(Effect.andThen(Users, (_) => _.getAll)) * return (yield* Fiber.join(fiber)).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 * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const runtime: (self: FiberSet) => () => Effect.Effect<(effect: Effect.Effect, options?: (Effect.RunOptions & { readonly propagateInterruption?: boolean | undefined; }) | undefined) => Fiber.Fiber, never, R>; /** * Captures a `Runtime` and returns a Promise-based runner that forks effects * into the `FiberSet`. * * **When to use** * * Use when you need to bridge effects to `Promise` values while still tracking * their fibers in a `FiberSet`. * * **Details** * * The returned run function returns a `Promise` for each effect result. * * **Example** (Running effects as promises) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * const runPromise = yield* FiberSet.runtimePromise(set)() * * // Run effects as promises * const promise1 = runPromise(Effect.succeed("hello")) * const promise2 = runPromise(Effect.succeed("world")) * * return [yield* Effect.promise(() => promise1), yield* Effect.promise(() => promise2)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => ["hello", "world"] * ``` * * @see {@link runtime} for a runner that returns the forked `Fiber` * * @stability stable * @category combinators * @since 3.13.0 */ export declare const runtimePromise: (self: FiberSet) => () => Effect.Effect<(effect: Effect.Effect, options?: (Effect.RunOptions & { readonly propagateInterruption?: boolean | undefined; }) | undefined) => Promise, never, R>; /** * Gets the number of fibers currently in the FiberSet. * * **Example** (Checking the set size) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * const sizeBefore = yield* FiberSet.size(set) * * // Add some fibers * yield* FiberSet.run(set, Effect.never) * yield* FiberSet.run(set, Effect.never) * * return [sizeBefore, yield* FiberSet.size(set)] * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => [0, 2] * ``` * * @stability stable * @category combinators * @since 2.0.0 */ export declare const size: (self: FiberSet) => Effect.Effect; /** * Joins all fibers in the FiberSet. If any fiber in the set terminates with a failure, * the returned Effect will terminate with the first failure that occurred. * * **Example** (Joining failing fibers) * * ```ts import.meta.vitest * import { Effect, Exit, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * yield* FiberSet.add(set, Effect.runFork(Effect.fail("error"))) * * // parent fiber will fail with "error" * yield* FiberSet.join(set) * }) * * 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: FiberSet) => Effect.Effect; /** * Waits until the fiber set is empty. * * **Example** (Waiting for an empty set) * * ```ts import.meta.vitest * import { Effect, FiberSet } from "effect" * * const program = Effect.gen(function*() { * const set = yield* FiberSet.make() * * yield* FiberSet.run(set, Effect.yieldNow) * yield* FiberSet.run(set, Effect.yieldNow) * * // Wait for all fibers to complete * yield* FiberSet.awaitEmpty(set) * * return yield* FiberSet.size(set) * }) * * const actual = await Effect.runPromise(Effect.scoped(program)) * actual // => 0 * ``` * * @stability stable * @category combinators * @since 3.13.0 */ export declare const awaitEmpty: (self: FiberSet) => Effect.Effect; export {}; //# sourceMappingURL=FiberSet.d.ts.map