/** * @since 2.0.0 */ import type * as Cause from "./Cause.js" import type * as Effect from "./Effect.js" import type * as Exit from "./Exit.js" import type * as FiberId from "./FiberId.js" import type { LazyArg } from "./Function.js" import * as core from "./internal/core.js" import * as internal from "./internal/deferred.js" import type * as MutableRef from "./MutableRef.js" import type * as Option from "./Option.js" import type * as Types from "./Types.js" import type * as Unify from "./Unify.js" /** * @since 2.0.0 * @category symbols */ export const DeferredTypeId: unique symbol = internal.DeferredTypeId /** * @since 2.0.0 * @category symbols */ export type DeferredTypeId = typeof DeferredTypeId /** * A `Deferred` represents an asynchronous variable that can be set exactly * once, with the ability for an arbitrary number of fibers to suspend (by * calling `Deferred.await`) and automatically resume when the variable is set. * * `Deferred` can be used for building primitive actions whose completions * require the coordinated action of multiple fibers, and for building * higher-level concurrent or asynchronous structures. * * @since 2.0.0 * @category models */ export interface Deferred extends Effect.Effect, Deferred.Variance { /** @internal */ readonly state: MutableRef.MutableRef> /** @internal */ readonly blockingOn: FiberId.FiberId readonly [Unify.typeSymbol]?: unknown readonly [Unify.unifySymbol]?: DeferredUnify readonly [Unify.ignoreSymbol]?: DeferredUnifyIgnore } /** * @category models * @since 3.8.0 */ export interface DeferredUnify extends Effect.EffectUnify { Deferred?: () => Extract> } /** * @category models * @since 3.8.0 */ export interface DeferredUnifyIgnore extends Effect.EffectUnifyIgnore { Effect?: true } /** * @since 2.0.0 */ export declare namespace Deferred { /** * @since 2.0.0 * @category models */ export interface Variance { readonly [DeferredTypeId]: { readonly _A: Types.Invariant readonly _E: Types.Invariant } } } /** * Creates a new `Deferred`. * * @since 2.0.0 * @category constructors */ export const make: () => Effect.Effect> = core.deferredMake /** * Creates a new `Deferred` from the specified `FiberId`. * * @since 2.0.0 * @category constructors */ export const makeAs: (fiberId: FiberId.FiberId) => Effect.Effect> = core.deferredMakeAs const _await: (self: Deferred) => Effect.Effect = core.deferredAwait export { /** * Retrieves the value of the `Deferred`, suspending the fiber running the * workflow until the result is available. * * @since 2.0.0 * @category getters */ _await as await } /** * Completes the deferred with the result of the specified effect. If the * deferred has already been completed, the method will produce false. * * Note that `Deferred.completeWith` will be much faster, so consider using * that if you do not need to memoize the result of the specified effect. * * @since 2.0.0 * @category utils */ export const complete: { /** * Completes the deferred with the result of the specified effect. If the * deferred has already been completed, the method will produce false. * * Note that `Deferred.completeWith` will be much faster, so consider using * that if you do not need to memoize the result of the specified effect. * * @since 2.0.0 * @category utils */ (effect: Effect.Effect): (self: Deferred) => Effect.Effect /** * Completes the deferred with the result of the specified effect. If the * deferred has already been completed, the method will produce false. * * Note that `Deferred.completeWith` will be much faster, so consider using * that if you do not need to memoize the result of the specified effect. * * @since 2.0.0 * @category utils */ (self: Deferred, effect: Effect.Effect): Effect.Effect } = core.deferredComplete /** * Completes the deferred with the result of the specified effect. If the * deferred has already been completed, the method will produce false. * * @since 2.0.0 * @category utils */ export const completeWith: { /** * Completes the deferred with the result of the specified effect. If the * deferred has already been completed, the method will produce false. * * @since 2.0.0 * @category utils */ (effect: Effect.Effect): (self: Deferred) => Effect.Effect /** * Completes the deferred with the result of the specified effect. If the * deferred has already been completed, the method will produce false. * * @since 2.0.0 * @category utils */ (self: Deferred, effect: Effect.Effect): Effect.Effect } = core.deferredCompleteWith /** * Exits the `Deferred` with the specified `Exit` value, which will be * propagated to all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const done: { /** * Exits the `Deferred` with the specified `Exit` value, which will be * propagated to all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (exit: Exit.Exit): (self: Deferred) => Effect.Effect /** * Exits the `Deferred` with the specified `Exit` value, which will be * propagated to all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, exit: Exit.Exit): Effect.Effect } = core.deferredDone /** * Fails the `Deferred` with the specified error, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const fail: { /** * Fails the `Deferred` with the specified error, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (error: E): (self: Deferred) => Effect.Effect /** * Fails the `Deferred` with the specified error, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, error: E): Effect.Effect } = core.deferredFail /** * Fails the `Deferred` with the specified error, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const failSync: { /** * Fails the `Deferred` with the specified error, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (evaluate: LazyArg): (self: Deferred) => Effect.Effect /** * Fails the `Deferred` with the specified error, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, evaluate: LazyArg): Effect.Effect } = core.deferredFailSync /** * Fails the `Deferred` with the specified `Cause`, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const failCause: { /** * Fails the `Deferred` with the specified `Cause`, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (cause: Cause.Cause): (self: Deferred) => Effect.Effect /** * Fails the `Deferred` with the specified `Cause`, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, cause: Cause.Cause): Effect.Effect } = core.deferredFailCause /** * Fails the `Deferred` with the specified `Cause`, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const failCauseSync: { /** * Fails the `Deferred` with the specified `Cause`, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (evaluate: LazyArg>): (self: Deferred) => Effect.Effect /** * Fails the `Deferred` with the specified `Cause`, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, evaluate: LazyArg>): Effect.Effect } = core.deferredFailCauseSync /** * Kills the `Deferred` with the specified defect, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const die: { /** * Kills the `Deferred` with the specified defect, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (defect: unknown): (self: Deferred) => Effect.Effect /** * Kills the `Deferred` with the specified defect, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, defect: unknown): Effect.Effect } = core.deferredDie /** * Kills the `Deferred` with the specified defect, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ export const dieSync: { /** * Kills the `Deferred` with the specified defect, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (evaluate: LazyArg): (self: Deferred) => Effect.Effect /** * Kills the `Deferred` with the specified defect, which will be propagated to * all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category utils */ (self: Deferred, evaluate: LazyArg): Effect.Effect } = core.deferredDieSync /** * Completes the `Deferred` with interruption. This will interrupt all fibers * waiting on the value of the `Deferred` with the `FiberId` of the fiber * calling this method. * * @since 2.0.0 * @category utils */ export const interrupt: (self: Deferred) => Effect.Effect = core.deferredInterrupt /** * Completes the `Deferred` with interruption. This will interrupt all fibers * waiting on the value of the `Deferred` with the specified `FiberId`. * * @since 2.0.0 * @category utils */ export const interruptWith: { /** * Completes the `Deferred` with interruption. This will interrupt all fibers * waiting on the value of the `Deferred` with the specified `FiberId`. * * @since 2.0.0 * @category utils */ (fiberId: FiberId.FiberId): (self: Deferred) => Effect.Effect /** * Completes the `Deferred` with interruption. This will interrupt all fibers * waiting on the value of the `Deferred` with the specified `FiberId`. * * @since 2.0.0 * @category utils */ (self: Deferred, fiberId: FiberId.FiberId): Effect.Effect } = core.deferredInterruptWith /** * Returns `true` if this `Deferred` has already been completed with a value or * an error, `false` otherwise. * * @since 2.0.0 * @category getters */ export const isDone: (self: Deferred) => Effect.Effect = core.deferredIsDone /** * Returns a `Some>` from the `Deferred` if this `Deferred` has * already been completed, `None` otherwise. * * @since 2.0.0 * @category getters */ export const poll: ( self: Deferred ) => Effect.Effect>> = core.deferredPoll /** * Completes the `Deferred` with the specified value. * * @since 2.0.0 * @category utils */ export const succeed: { /** * Completes the `Deferred` with the specified value. * * @since 2.0.0 * @category utils */ (value: A): (self: Deferred) => Effect.Effect /** * Completes the `Deferred` with the specified value. * * @since 2.0.0 * @category utils */ (self: Deferred, value: A): Effect.Effect } = core.deferredSucceed /** * Completes the `Deferred` with the specified lazily evaluated value. * * @since 2.0.0 * @category utils */ export const sync: { /** * Completes the `Deferred` with the specified lazily evaluated value. * * @since 2.0.0 * @category utils */ (evaluate: LazyArg): (self: Deferred) => Effect.Effect /** * Completes the `Deferred` with the specified lazily evaluated value. * * @since 2.0.0 * @category utils */ (self: Deferred, evaluate: LazyArg): Effect.Effect } = core.deferredSync /** * Unsafely creates a new `Deferred` from the specified `FiberId`. * * @since 2.0.0 * @category unsafe */ export const unsafeMake: (fiberId: FiberId.FiberId) => Deferred = core.deferredUnsafeMake /** * Unsafely exits the `Deferred` with the specified `Exit` value, which will be * propagated to all fibers waiting on the value of the `Deferred`. * * @since 2.0.0 * @category unsafe */ export const unsafeDone: (self: Deferred, effect: Effect.Effect) => void = core.deferredUnsafeDone