/** * @since 2.0.0 */ import type { Cause } from "./Cause.js" import type * as Context from "./Context.js" import type * as Effect from "./Effect.js" import type * as Exit from "./Exit.js" import type * as Fiber from "./Fiber.js" import type * as FiberId from "./FiberId.js" import type * as FiberRef from "./FiberRef.js" import type * as FiberRefs from "./FiberRefs.js" import type { Inspectable } from "./Inspectable.js" import * as internal from "./internal/runtime.js" import type { Pipeable } from "./Pipeable.js" import type * as RuntimeFlags from "./RuntimeFlags.js" import type { Scheduler } from "./Scheduler.js" import type { Scope } from "./Scope.js" /** * @since 2.0.0 * @category models */ export interface AsyncFiberException { readonly _tag: "AsyncFiberException" readonly fiber: Fiber.RuntimeFiber } /** * @since 2.0.0 * @category models */ export interface Cancel { (fiberId?: FiberId.FiberId, options?: RunCallbackOptions | undefined): void } /** * @since 2.0.0 * @category models */ export interface Runtime extends Pipeable { /** * The context used as initial for forks */ readonly context: Context.Context /** * The runtime flags used as initial for forks */ readonly runtimeFlags: RuntimeFlags.RuntimeFlags /** * The fiber references used as initial for forks */ readonly fiberRefs: FiberRefs.FiberRefs } /** * @since 3.12.0 */ export declare namespace Runtime { /** * @since 3.12.0 * @category Type Extractors */ export type Context> = [T] extends [Runtime] ? R : never } /** * @since 2.0.0 * @category models */ export interface RunForkOptions { readonly scheduler?: Scheduler | undefined readonly updateRefs?: ((refs: FiberRefs.FiberRefs, fiberId: FiberId.Runtime) => FiberRefs.FiberRefs) | undefined readonly immediate?: boolean readonly scope?: Scope } /** * Executes the effect using the provided Scheduler or using the global * Scheduler if not provided * * @since 2.0.0 * @category execution */ export const runFork: { /** * Executes the effect using the provided Scheduler or using the global * Scheduler if not provided * * @since 2.0.0 * @category execution */ (runtime: Runtime): (effect: Effect.Effect, options?: RunForkOptions | undefined) => Fiber.RuntimeFiber /** * Executes the effect using the provided Scheduler or using the global * Scheduler if not provided * * @since 2.0.0 * @category execution */ ( runtime: Runtime, effect: Effect.Effect, options?: RunForkOptions | undefined ): Fiber.RuntimeFiber } = internal.unsafeFork /** * Executes the effect synchronously returning the exit. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ export const runSyncExit: { /** * Executes the effect synchronously returning the exit. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime, effect: Effect.Effect): Exit.Exit /** * Executes the effect synchronously returning the exit. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime): (effect: Effect.Effect) => Exit.Exit } = internal.unsafeRunSyncExit /** * Executes the effect synchronously throwing in case of errors or async boundaries. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ export const runSync: { /** * Executes the effect synchronously throwing in case of errors or async boundaries. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime, effect: Effect.Effect): A /** * Executes the effect synchronously throwing in case of errors or async boundaries. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime): (effect: Effect.Effect) => A } = internal.unsafeRunSync /** * @since 2.0.0 * @category models */ export interface RunCallbackOptions extends RunForkOptions { readonly onExit?: ((exit: Exit.Exit) => void) | undefined } /** * Executes the effect asynchronously, eventually passing the exit value to * the specified callback. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ export const runCallback: { /** * Executes the effect asynchronously, eventually passing the exit value to * the specified callback. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime): ( effect: Effect.Effect, options?: RunCallbackOptions | undefined ) => (fiberId?: FiberId.FiberId, options?: RunCallbackOptions | undefined) => void /** * Executes the effect asynchronously, eventually passing the exit value to * the specified callback. * * This method is effectful and should only be invoked at the edges of your * program. * * @since 2.0.0 * @category execution */ ( runtime: Runtime, effect: Effect.Effect, options?: RunCallbackOptions | undefined ): (fiberId?: FiberId.FiberId, options?: RunCallbackOptions | undefined) => void } = internal.unsafeRunCallback /** * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved * with the value of the effect once the effect has been executed, or will be * rejected with the first error or exception throw by the effect. * * This method is effectful and should only be used at the edges of your * program. * * @since 2.0.0 * @category execution */ export const runPromise: { /** * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved * with the value of the effect once the effect has been executed, or will be * rejected with the first error or exception throw by the effect. * * This method is effectful and should only be used at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime): (effect: Effect.Effect, options?: { readonly signal?: AbortSignal } | undefined) => Promise /** * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved * with the value of the effect once the effect has been executed, or will be * rejected with the first error or exception throw by the effect. * * This method is effectful and should only be used at the edges of your * program. * * @since 2.0.0 * @category execution */ ( runtime: Runtime, effect: Effect.Effect, options?: { readonly signal?: AbortSignal } | undefined ): Promise } = internal.unsafeRunPromise /** * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved * with the `Exit` state of the effect once the effect has been executed. * * This method is effectful and should only be used at the edges of your * program. * * @since 2.0.0 * @category execution */ export const runPromiseExit: { /** * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved * with the `Exit` state of the effect once the effect has been executed. * * This method is effectful and should only be used at the edges of your * program. * * @since 2.0.0 * @category execution */ (runtime: Runtime): ( effect: Effect.Effect, options?: { readonly signal?: AbortSignal } | undefined ) => Promise> /** * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved * with the `Exit` state of the effect once the effect has been executed. * * This method is effectful and should only be used at the edges of your * program. * * @since 2.0.0 * @category execution */ ( runtime: Runtime, effect: Effect.Effect, options?: { readonly signal?: AbortSignal } | undefined ): Promise> } = internal.unsafeRunPromiseExit /** * @since 2.0.0 * @category constructors */ export const defaultRuntime: Runtime = internal.defaultRuntime /** * @since 2.0.0 * @category constructors */ export const defaultRuntimeFlags: RuntimeFlags.RuntimeFlags = internal.defaultRuntimeFlags /** * @since 2.0.0 * @category constructors */ export const make: ( options: { readonly context: Context.Context readonly runtimeFlags: RuntimeFlags.RuntimeFlags readonly fiberRefs: FiberRefs.FiberRefs } ) => Runtime = internal.make /** * @since 2.0.0 * @category symbols */ export const FiberFailureId = Symbol.for("effect/Runtime/FiberFailure") /** * @since 2.0.0 * @category symbols */ export type FiberFailureId = typeof FiberFailureId /** * @since 2.0.0 * @category symbols */ export const FiberFailureCauseId: unique symbol = internal.FiberFailureCauseId /** * @since 2.0.0 * @category exports */ export type FiberFailureCauseId = typeof FiberFailureCauseId /** * @since 2.0.0 * @category models */ export interface FiberFailure extends Error, Inspectable { readonly [FiberFailureId]: FiberFailureId readonly [FiberFailureCauseId]: Cause } /** * @since 2.0.0 * @category guards */ export const isAsyncFiberException: (u: unknown) => u is AsyncFiberException = internal.isAsyncFiberException /** * @since 2.0.0 * @category guards */ export const isFiberFailure: (u: unknown) => u is FiberFailure = internal.isFiberFailure /** * @since 2.0.0 * @category constructors */ export const makeFiberFailure: (cause: Cause) => FiberFailure = internal.fiberFailure /** * @since 2.0.0 * @category runtime flags */ export const updateRuntimeFlags: { /** * @since 2.0.0 * @category runtime flags */ (f: (flags: RuntimeFlags.RuntimeFlags) => RuntimeFlags.RuntimeFlags): (self: Runtime) => Runtime /** * @since 2.0.0 * @category runtime flags */ ( self: Runtime, f: (flags: RuntimeFlags.RuntimeFlags) => RuntimeFlags.RuntimeFlags ): Runtime } = internal.updateRuntimeFlags /** * @since 2.0.0 * @category runtime flags */ export const enableRuntimeFlag: { /** * @since 2.0.0 * @category runtime flags */ (flag: RuntimeFlags.RuntimeFlag): (self: Runtime) => Runtime /** * @since 2.0.0 * @category runtime flags */ (self: Runtime, flag: RuntimeFlags.RuntimeFlag): Runtime } = internal.enableRuntimeFlag /** * @since 2.0.0 * @category runtime flags */ export const disableRuntimeFlag: { /** * @since 2.0.0 * @category runtime flags */ (flag: RuntimeFlags.RuntimeFlag): (self: Runtime) => Runtime /** * @since 2.0.0 * @category runtime flags */ (self: Runtime, flag: RuntimeFlags.RuntimeFlag): Runtime } = internal.disableRuntimeFlag /** * @since 2.0.0 * @category context */ export const updateContext: { /** * @since 2.0.0 * @category context */ (f: (context: Context.Context) => Context.Context): (self: Runtime) => Runtime /** * @since 2.0.0 * @category context */ (self: Runtime, f: (context: Context.Context) => Context.Context): Runtime } = internal.updateContext /** * @since 2.0.0 * @category context * @example * ```ts * import { Context, Runtime } from "effect" * * class Name extends Context.Tag("Name")() {} * * const runtime: Runtime.Runtime = Runtime.defaultRuntime.pipe( * Runtime.provideService(Name, "John") * ) * ``` */ export const provideService: { /** * @since 2.0.0 * @category context * @example * ```ts * import { Context, Runtime } from "effect" * * class Name extends Context.Tag("Name")() {} * * const runtime: Runtime.Runtime = Runtime.defaultRuntime.pipe( * Runtime.provideService(Name, "John") * ) * ``` */ (tag: Context.Tag, service: S): (self: Runtime) => Runtime /** * @since 2.0.0 * @category context * @example * ```ts * import { Context, Runtime } from "effect" * * class Name extends Context.Tag("Name")() {} * * const runtime: Runtime.Runtime = Runtime.defaultRuntime.pipe( * Runtime.provideService(Name, "John") * ) * ``` */ (self: Runtime, tag: Context.Tag, service: S): Runtime } = internal.provideService /** * @since 2.0.0 * @category fiber refs */ export const updateFiberRefs: { /** * @since 2.0.0 * @category fiber refs */ (f: (fiberRefs: FiberRefs.FiberRefs) => FiberRefs.FiberRefs): (self: Runtime) => Runtime /** * @since 2.0.0 * @category fiber refs */ ( self: Runtime, f: (fiberRefs: FiberRefs.FiberRefs) => FiberRefs.FiberRefs ): Runtime } = internal.updateFiberRefs /** * @since 2.0.0 * @category fiber refs * @example * ```ts * import { Effect, FiberRef, Runtime } from "effect" * * const ref = FiberRef.unsafeMake(0) * * const updatedRuntime = Runtime.defaultRuntime.pipe( * Runtime.setFiberRef(ref, 1) * ) * * // returns 1 * const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)) * ``` */ export const setFiberRef: { /** * @since 2.0.0 * @category fiber refs * @example * ```ts * import { Effect, FiberRef, Runtime } from "effect" * * const ref = FiberRef.unsafeMake(0) * * const updatedRuntime = Runtime.defaultRuntime.pipe( * Runtime.setFiberRef(ref, 1) * ) * * // returns 1 * const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)) * ``` */ (fiberRef: FiberRef.FiberRef, value: A): (self: Runtime) => Runtime /** * @since 2.0.0 * @category fiber refs * @example * ```ts * import { Effect, FiberRef, Runtime } from "effect" * * const ref = FiberRef.unsafeMake(0) * * const updatedRuntime = Runtime.defaultRuntime.pipe( * Runtime.setFiberRef(ref, 1) * ) * * // returns 1 * const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)) * ``` */ (self: Runtime, fiberRef: FiberRef.FiberRef, value: A): Runtime } = internal.setFiberRef /** * @since 2.0.0 * @category fiber refs * @example * ```ts * import { Effect, FiberRef, Runtime } from "effect" * * const ref = FiberRef.unsafeMake(0) * * const updatedRuntime = Runtime.defaultRuntime.pipe( * Runtime.setFiberRef(ref, 1), * Runtime.deleteFiberRef(ref) * ) * * // returns 0 * const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)) * ``` */ export const deleteFiberRef: { /** * @since 2.0.0 * @category fiber refs * @example * ```ts * import { Effect, FiberRef, Runtime } from "effect" * * const ref = FiberRef.unsafeMake(0) * * const updatedRuntime = Runtime.defaultRuntime.pipe( * Runtime.setFiberRef(ref, 1), * Runtime.deleteFiberRef(ref) * ) * * // returns 0 * const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)) * ``` */ (fiberRef: FiberRef.FiberRef): (self: Runtime) => Runtime /** * @since 2.0.0 * @category fiber refs * @example * ```ts * import { Effect, FiberRef, Runtime } from "effect" * * const ref = FiberRef.unsafeMake(0) * * const updatedRuntime = Runtime.defaultRuntime.pipe( * Runtime.setFiberRef(ref, 1), * Runtime.deleteFiberRef(ref) * ) * * // returns 0 * const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)) * ``` */ (self: Runtime, fiberRef: FiberRef.FiberRef): Runtime } = internal.deleteFiberRef