/** * @since 1.0.0 */ import type * as Either from "@effect/data/Either" import type * as HashSet from "@effect/data/HashSet" import type * as Option from "@effect/data/Option" import type * as order from "@effect/data/Order" import type { Pipeable } from "@effect/data/Pipeable" import type * as Cause from "@effect/io/Cause" import type * as Effect from "@effect/io/Effect" import type * as Exit from "@effect/io/Exit" import type * as FiberId from "@effect/io/FiberId" import type { FiberRef } from "@effect/io/FiberRef" import type * as FiberRefs from "@effect/io/FiberRefs" import type * as FiberStatus from "@effect/io/FiberStatus" import * as core from "@effect/io/internal/core" import * as circular from "@effect/io/internal/effect/circular" import * as internal from "@effect/io/internal/fiber" import * as fiberRuntime from "@effect/io/internal/fiberRuntime" import type * as RuntimeFlags from "@effect/io/RuntimeFlags" import type * as Scope from "@effect/io/Scope" /** * @since 1.0.0 * @category symbols */ export const FiberTypeId: unique symbol = internal.FiberTypeId /** * @since 1.0.0 * @category symbols */ export type FiberTypeId = typeof FiberTypeId /** * @since 1.0.0 * @category symbols */ export const RuntimeFiberTypeId: unique symbol = internal.RuntimeFiberTypeId /** * @since 1.0.0 * @category symbols */ export type RuntimeFiberTypeId = typeof RuntimeFiberTypeId /** * A fiber is a lightweight thread of execution that never consumes more than a * whole thread (but may consume much less, depending on contention and * asynchronicity). Fibers are spawned by forking effects, which run * concurrently with the parent effect. * * Fibers can be joined, yielding their result to other fibers, or interrupted, * which terminates the fiber, safely releasing all resources. * * @since 1.0.0 * @category models */ export interface Fiber extends Fiber.Variance, Pipeable { /** * The identity of the fiber. */ id(): FiberId.FiberId /** * Awaits the fiber, which suspends the awaiting fiber until the result of the * fiber has been determined. */ await(): Effect.Effect> /** * Retrieves the immediate children of the fiber. */ children(): Effect.Effect>> /** * Inherits values from all `FiberRef` instances into current fiber. This * will resume immediately. */ inheritAll(): Effect.Effect /** * Tentatively observes the fiber, but returns immediately if it is not * already done. */ poll(): Effect.Effect>> /** * In the background, interrupts the fiber as if interrupted from the * specified fiber. If the fiber has already exited, the returned effect will * resume immediately. Otherwise, the effect will resume when the fiber exits. */ interruptAsFork(fiberId: FiberId.FiberId): Effect.Effect } /** * A runtime fiber that is executing an effect. Runtime fibers have an * identity and a trace. * * @since 1.0.0 * @category models */ export interface RuntimeFiber extends Fiber, Fiber.RuntimeVariance { /** * Reads the current number of ops that have occurred since the last yield */ get currentOpCount(): number /** * Reads the current value of a fiber ref */ getFiberRef(fiberRef: FiberRef): X /** * The identity of the fiber. */ id(): FiberId.Runtime /** * The status of the fiber. */ status(): Effect.Effect /** * Returns the current `RuntimeFlags` the fiber is running with. */ runtimeFlags(): Effect.Effect /** * Adds an observer to the list of observers. */ addObserver(observer: (exit: Exit.Exit) => void): void /** * Removes the specified observer from the list of observers that will be * notified when the fiber exits. */ removeObserver(observer: (exit: Exit.Exit) => void): void /** * Retrieves all fiber refs of the fiber. */ getFiberRefs(): FiberRefs.FiberRefs /** * Unsafely observes the fiber, but returns immediately if it is not * already done. */ unsafePoll(): Exit.Exit | null } /** * @since 1.0.0 */ export declare namespace Fiber { /** * @since 1.0.0 * @category models */ export type Runtime = RuntimeFiber /** * @since 1.0.0 * @category models */ export interface Variance { readonly [FiberTypeId]: { readonly _E: (_: never) => E readonly _A: (_: never) => A } } /** * @since 1.0.0 */ export interface RuntimeVariance { readonly [RuntimeFiberTypeId]: { readonly _E: (_: never) => E readonly _A: (_: never) => A } } /** * @since 1.0.0 * @category models */ export interface Dump { /** * The fiber's unique identifier. */ readonly id: FiberId.Runtime /** * The status of the fiber. */ readonly status: FiberStatus.FiberStatus } /** * A record containing information about a `Fiber`. * * @since 1.0.0 * @category models */ export interface Descriptor { /** * The fiber's unique identifier. */ readonly id: FiberId.FiberId /** * The status of the fiber. */ readonly status: FiberStatus.FiberStatus /** * The set of fibers attempting to interrupt the fiber or its ancestors. */ readonly interruptors: HashSet.HashSet } } /** * @since 1.0.0 * @category instances */ export const Order: order.Order> = internal.Order /** * Returns `true` if the specified value is a `Fiber`, `false` otherwise. * * @since 1.0.0 * @category refinements */ export const isFiber: (u: unknown) => u is Fiber = internal.isFiber /** * Returns `true` if the specified `Fiber` is a `RuntimeFiber`, `false` * otherwise. * * @since 1.0.0 * @category refinements */ export const isRuntimeFiber: (self: Fiber) => self is RuntimeFiber = internal.isRuntimeFiber /** * The identity of the fiber. * * @since 1.0.0 * @category getters */ export const id: (self: Fiber) => FiberId.FiberId = internal.id const _await: (self: Fiber) => Effect.Effect> = internal._await export { /** * Awaits the fiber, which suspends the awaiting fiber until the result of the * fiber has been determined. * * @since 1.0.0 * @category getters */ _await as await } /** * Awaits on all fibers to be completed, successfully or not. * * @since 1.0.0 * @category destructors */ export const awaitAll: (fibers: Iterable>) => Effect.Effect = fiberRuntime.fiberAwaitAll /** * Retrieves the immediate children of the fiber. * * @since 1.0.0 * @category getters */ export const children: (self: Fiber) => Effect.Effect>> = internal.children /** * Collects all fibers into a single fiber producing an in-order list of the * results. * * @since 1.0.0 * @category constructors */ export const all: (fibers: Iterable>) => Fiber> = fiberRuntime.fiberAll /** * A fiber that is done with the specified `Exit` value. * * @since 1.0.0 * @category constructors */ export const done: (exit: Exit.Exit) => Fiber = internal.done /** * @since 1.0.0 * @category destructors */ export const dump: (self: RuntimeFiber) => Effect.Effect = internal.dump /** * @since 1.0.0 * @category destructors */ export const dumpAll: ( fibers: Iterable> ) => Effect.Effect> = internal.dumpAll /** * A fiber that has already failed with the specified value. * * @since 1.0.0 * @category constructors */ export const fail: (error: E) => Fiber = internal.fail /** * Creates a `Fiber` that has already failed with the specified cause. * * @since 1.0.0 * @category constructors */ export const failCause: (cause: Cause.Cause) => Fiber = internal.failCause /** * Lifts an `Effect` into a `Fiber`. * * @since 1.0.0 * @category conversions */ export const fromEffect: (effect: Effect.Effect) => Effect.Effect> = internal.fromEffect /** * Gets the current fiber if one is running. * * @since 1.0.0 * @category utilities */ export const getCurrentFiber: () => Option.Option> = internal.getCurrentFiber /** * Inherits values from all `FiberRef` instances into current fiber. This * will resume immediately. * * @since 1.0.0 * @category destructors */ export const inheritAll: (self: Fiber) => Effect.Effect = internal.inheritAll /** * Interrupts the fiber from whichever fiber is calling this method. If the * fiber has already exited, the returned effect will resume immediately. * Otherwise, the effect will resume when the fiber exits. * * @since 1.0.0 * @category interruption */ export const interrupt: (self: Fiber) => Effect.Effect> = core.interruptFiber /** * Constructrs a `Fiber` that is already interrupted. * * @since 1.0.0 * @category constructors */ export const interrupted: (fiberId: FiberId.FiberId) => Fiber = internal.interrupted /** * Interrupts the fiber as if interrupted from the specified fiber. If the * fiber has already exited, the returned effect will resume immediately. * Otherwise, the effect will resume when the fiber exits. * * @since 1.0.0 * @category interruption */ export const interruptAs: { (fiberId: FiberId.FiberId): (self: Fiber) => Effect.Effect> (self: Fiber, fiberId: FiberId.FiberId): Effect.Effect> } = core.interruptAsFiber /** * Interrupts the fiber as if interrupted from the specified fiber. If the * fiber has already exited, the returned effect will resume immediately. * Otherwise, the effect will resume when the fiber exits. * * @since 1.0.0 * @category interruption */ export const interruptAsFork: { (fiberId: FiberId.FiberId): (self: Fiber) => Effect.Effect (self: Fiber, fiberId: FiberId.FiberId): Effect.Effect } = internal.interruptAsFork /** * Interrupts all fibers, awaiting their interruption. * * @since 1.0.0 * @category interruption */ export const interruptAll: (fibers: Iterable>) => Effect.Effect = internal.interruptAll /** * Interrupts all fibers as by the specified fiber, awaiting their * interruption. * * @since 1.0.0 * @category interruption */ export const interruptAllAs: { (fiberId: FiberId.FiberId): (fibers: Iterable>) => Effect.Effect (fibers: Iterable>, fiberId: FiberId.FiberId): Effect.Effect } = internal.interruptAllAs /** * Interrupts the fiber from whichever fiber is calling this method. The * interruption will happen in a separate daemon fiber, and the returned * effect will always resume immediately without waiting. * * @since 1.0.0 * @category interruption */ export const interruptFork: (self: Fiber) => Effect.Effect = fiberRuntime.fiberInterruptFork /** * Joins the fiber, which suspends the joining fiber until the result of the * fiber has been determined. Attempting to join a fiber that has erred will * result in a catchable error. Joining an interrupted fiber will result in an * "inner interruption" of this fiber, unlike interruption triggered by * another fiber, "inner interruption" can be caught and recovered. * * @since 1.0.0 * @category destructors */ export const join: (self: Fiber) => Effect.Effect = internal.join /** * Joins all fibers, awaiting their _successful_ completion. Attempting to * join a fiber that has erred will result in a catchable error, _if_ that * error does not result from interruption. * * @since 1.0.0 * @category destructors */ export const joinAll: (fibers: Iterable>) => Effect.Effect = fiberRuntime.fiberJoinAll /** * Maps over the value the Fiber computes. * * @since 1.0.0 * @category mapping */ export const map: { (f: (a: A) => B): (self: Fiber) => Fiber (self: Fiber, f: (a: A) => B): Fiber } = internal.map /** * Effectually maps over the value the fiber computes. * * @since 1.0.0 * @category mapping */ export const mapEffect: { (f: (a: A) => Effect.Effect): (self: Fiber) => Fiber (self: Fiber, f: (a: A) => Effect.Effect): Fiber } = internal.mapEffect /** * Passes the success of this fiber to the specified callback, and continues * with the fiber that it returns. * * @since 1.0.0 * @category mapping */ export const mapFiber: { (f: (a: A) => Fiber): (self: Fiber) => Effect.Effect> (self: Fiber, f: (a: A) => Fiber): Effect.Effect> } = internal.mapFiber /** * Folds over the `Fiber` or `RuntimeFiber`. * * @since 1.0.0 * @category folding */ export const match: { ( options: { readonly onFiber: (fiber: Fiber) => Z; readonly onRuntimeFiber: (fiber: RuntimeFiber) => Z } ): (self: Fiber) => Z ( self: Fiber, options: { readonly onFiber: (fiber: Fiber) => Z; readonly onRuntimeFiber: (fiber: RuntimeFiber) => Z } ): Z } = internal.match /** * A fiber that never fails or succeeds. * * @since 1.0.0 * @category constructors */ export const never: Fiber = internal.never /** * Returns a fiber that prefers `this` fiber, but falls back to the `that` one * when `this` one fails. Interrupting the returned fiber will interrupt both * fibers, sequentially, from left to right. * * @since 1.0.0 * @category alternatives */ export const orElse: { (that: Fiber): (self: Fiber) => Fiber (self: Fiber, that: Fiber): Fiber } = internal.orElse /** * Returns a fiber that prefers `this` fiber, but falls back to the `that` one * when `this` one fails. Interrupting the returned fiber will interrupt both * fibers, sequentially, from left to right. * * @since 1.0.0 * @category alternatives */ export const orElseEither: { (that: Fiber): (self: Fiber) => Fiber> (self: Fiber, that: Fiber): Fiber> } = internal.orElseEither /** * Tentatively observes the fiber, but returns immediately if it is not * already done. * * @since 1.0.0 * @category getters */ export const poll: (self: Fiber) => Effect.Effect>> = internal.poll /** * Pretty-prints a `RuntimeFiber`. * * @since 1.0.0 * @category destructors */ export const pretty: (self: RuntimeFiber) => Effect.Effect = internal.pretty /** * Returns a chunk containing all root fibers. * * @since 1.0.0 * @category constructors */ export const roots: Effect.Effect>> = internal.roots /** * Returns a chunk containing all root fibers. * * @since 1.0.0 * @category constructors */ export const unsafeRoots: (_: void) => Array> = internal.unsafeRoots /** * Converts this fiber into a scoped effect. The fiber is interrupted when the * scope is closed. * * @since 1.0.0 * @category destructors */ export const scoped: (self: Fiber) => Effect.Effect> = fiberRuntime.fiberScoped /** * Returns the `FiberStatus` of a `RuntimeFiber`. * * @since 1.0.0 * @category getters */ export const status: (self: RuntimeFiber) => Effect.Effect = internal.status /** * Returns a fiber that has already succeeded with the specified value. * * @since 1.0.0 * @category constructors */ export const succeed: (value: A) => Fiber = internal.succeed /** * A fiber that has already succeeded with unit. * * @since 1.0.0 * @category constructors */ export const unit: Fiber = internal.unit /** * Zips this fiber and the specified fiber together, producing a tuple of * their output. * * @since 1.0.0 * @category zipping */ export const zip: { (that: Fiber): (self: Fiber) => Fiber (self: Fiber, that: Fiber): Fiber } = circular.zipFiber /** * Same as `zip` but discards the output of that `Fiber`. * * @since 1.0.0 * @category zipping */ export const zipLeft: { (that: Fiber): (self: Fiber) => Fiber (self: Fiber, that: Fiber): Fiber } = circular.zipLeftFiber /** * Same as `zip` but discards the output of this `Fiber`. * * @since 1.0.0 * @category zipping */ export const zipRight: { (that: Fiber): (self: Fiber) => Fiber (self: Fiber, that: Fiber): Fiber } = circular.zipRightFiber /** * Zips this fiber with the specified fiber, combining their results using the * specified combiner function. Both joins and interruptions are performed in * sequential order from left to right. * * @since 1.0.0 * @category zipping */ export const zipWith: { (that: Fiber, f: (a: A, b: B) => C): (self: Fiber) => Fiber (self: Fiber, that: Fiber, f: (a: A, b: B) => C): Fiber } = circular.zipWithFiber