/** * @since 1.0.0 */ import type { LazyArg } from "@effect/data/Function" import type * as MutableRef from "@effect/data/MutableRef" import type * as Option from "@effect/data/Option" 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 * as core from "@effect/io/internal/core" import * as internal from "@effect/io/internal/deferred" /** * @since 1.0.0 * @category symbols */ export const DeferredTypeId: unique symbol = internal.DeferredTypeId /** * @since 1.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 1.0.0 * @category models */ export interface Deferred extends Deferred.Variance, Pipeable { /** @internal */ readonly state: MutableRef.MutableRef> /** @internal */ readonly blockingOn: FiberId.FiberId } /** * @since 1.0.0 */ export declare namespace Deferred { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [DeferredTypeId]: { readonly _E: (_: never) => E readonly _A: (_: never) => A } } } /** * Creates a new `Deferred`. * * @since 1.0.0 * @category constructors */ export const make: () => Effect.Effect> = core.deferredMake /** * Creates a new `Deferred` from the specified `FiberId`. * * @since 1.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 1.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 1.0.0 * @category utils */ export const complete: { (effect: Effect.Effect): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const completeWith: { (effect: Effect.Effect): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const done: { (exit: Exit.Exit): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const fail: { (error: E): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const failSync: { (evaluate: LazyArg): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const failCause: { (cause: Cause.Cause): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const failCauseSync: { (evaluate: LazyArg>): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const die: { (defect: unknown): (self: Deferred) => Effect.Effect (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 1.0.0 * @category utils */ export const dieSync: { (evaluate: LazyArg): (self: Deferred) => Effect.Effect (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 1.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 1.0.0 * @category utils */ export const interruptWith: { (fiberId: FiberId.FiberId): (self: Deferred) => Effect.Effect (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 1.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 1.0.0 * @category getters */ export const poll: ( self: Deferred ) => Effect.Effect>> = core.deferredPoll /** * Completes the `Deferred` with the specified value. * * @since 1.0.0 * @category utils */ export const succeed: { (value: A): (self: Deferred) => Effect.Effect (self: Deferred, value: A): Effect.Effect } = core.deferredSucceed /** * Completes the `Deferred` with the specified lazily evaluated value. * * @since 1.0.0 * @category utils */ export const sync: { (evaluate: LazyArg): (self: Deferred) => Effect.Effect (self: Deferred, evaluate: LazyArg): Effect.Effect } = core.deferredSync /** * Unsafely creates a new `Deferred` from the specified `FiberId`. * * @since 1.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 1.0.0 * @category unsafe */ export const unsafeDone: (self: Deferred, effect: Effect.Effect) => void = core.deferredUnsafeDone