Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Runtime

31 exports Since v2.0.0 Source

Constructors

Signature

declare const defaultRuntime: Runtime<never>

Signature

declare const defaultRuntimeFlags: RuntimeFlags.RuntimeFlags
Since v2.0.0

Signature

declare const make: <R>(options: {
readonly context: Context.Context<R>;
readonly fiberRefs: FiberRefs.FiberRefs;
readonly runtimeFlags: RuntimeFlags.RuntimeFlags;
}) => Runtime<R>

Signature

declare const makeFiberFailure: <E>(cause: Cause<E>) => FiberFailure

Context

Signature

declare const provideService: {
<I, S>(tag: Tag<I, S>, service: S): <R>(self: Runtime<R>) => Runtime<I | R>;
<R, I, S>(self: Runtime<R>, tag: Tag<I, S>, service: S): Runtime<R | I>;
}

Example

import { Context, Runtime } from "effect"
class Name extends Context.Tag("Name")<Name, string>() {}
const runtime: Runtime.Runtime<Name> = Runtime.defaultRuntime.pipe(
Runtime.provideService(Name, "John")
)

Signature

declare const updateContext: {
<R, R2>(f: (context: Context<R>) => Context<R2>): (self: Runtime<R>) => Runtime<R2>;
<R, R2>(self: Runtime<R>, f: (context: Context<R>) => Context<R2>): Runtime<R2>;
}

Execution

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.

Signature

declare const runCallback: {
<R>(runtime: Runtime<R>): <A, E>(effect: Effect<A, E, R>, options?: RunCallbackOptions<A, E>) => (fiberId?: FiberId, options?: RunCallbackOptions<A, E>) => void;
<R, A, E>(runtime: Runtime<R>, effect: Effect<A, E, R>, options?: RunCallbackOptions<A, E>): (fiberId?: FiberId, options?: RunCallbackOptions<A, E>) => void;
}
Since v2.0.0

Executes the effect using the provided Scheduler or using the global Scheduler if not provided

Signature

declare const runFork: {
<R>(runtime: Runtime<R>): <A, E>(effect: Effect<A, E, R>, options?: RunForkOptions) => RuntimeFiber<A, E>;
<R, A, E>(runtime: Runtime<R>, effect: Effect<A, E, R>, options?: RunForkOptions): RuntimeFiber<A, E>;
}

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.

Signature

declare const runPromise: {
<R>(runtime: Runtime<R>): <A, E>(effect: Effect<A, E, R>, options?: {
readonly signal?: AbortSignal;
}) => Promise<A>;
<R, A, E>(runtime: Runtime<R>, effect: Effect<A, E, R>, options?: {
readonly signal?: AbortSignal;
}): Promise<A>;
}

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.

Signature

declare const runPromiseExit: {
<R>(runtime: Runtime<R>): <A, E>(effect: Effect<A, E, R>, options?: {
readonly signal?: AbortSignal;
}) => Promise<Exit<A, E>>;
<R, A, E>(runtime: Runtime<R>, effect: Effect<A, E, R>, options?: {
readonly signal?: AbortSignal;
}): Promise<Exit<A, E>>;
}
Since v2.0.0

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.

Signature

declare const runSync: {
<A, E, R>(runtime: Runtime<R>, effect: Effect<A, E, R>): A;
<R>(runtime: Runtime<R>): <A, E>(effect: Effect<A, E, R>) => A;
}

Executes the effect synchronously returning the exit.

This method is effectful and should only be invoked at the edges of your program.

Signature

declare const runSyncExit: {
<A, E, R>(runtime: Runtime<R>, effect: Effect<A, E, R>): Exit<A, E>;
<R>(runtime: Runtime<R>): <A, E>(effect: Effect<A, E, R>) => Exit<A, E>;
}

Exports

Signature

type FiberFailureCauseId = typeof FiberFailureCauseId

Fiber Refs

Signature

declare const deleteFiberRef: {
<A>(fiberRef: FiberRef<A>): <R>(self: Runtime<R>) => Runtime<R>;
<R, A>(self: Runtime<R>, fiberRef: FiberRef<A>): Runtime<R>;
}

Example

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))

Signature

declare const setFiberRef: {
<A>(fiberRef: FiberRef<A>, value: A): <R>(self: Runtime<R>) => Runtime<R>;
<R, A>(self: Runtime<R>, fiberRef: FiberRef<A>, value: A): Runtime<R>;
}

Example

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))

Signature

declare const updateFiberRefs: {
(f: (fiberRefs: FiberRefs) => FiberRefs): <R>(self: Runtime<R>) => Runtime<R>;
<R>(self: Runtime<R>, f: (fiberRefs: FiberRefs) => FiberRefs): Runtime<R>;
}

Guards

Signature

declare const isAsyncFiberException: (u: unknown) => u is AsyncFiberException<unknown, unknown>

Signature

declare const isFiberFailure: (u: unknown) => u is FiberFailure

Models

Since v2.0.0

Signature

interface AsyncFiberException<out A, out E = never> {
readonly _tag: "AsyncFiberException";
readonly fiber: RuntimeFiber<A, E>;
}

Cancel

interface
Since v2.0.0

Signature

interface Cancel<out A, out E = never> {
(fiberId?: FiberId, options?: RunCallbackOptions<A, E>): void;
}

FiberFailure

interface
Since v2.0.0

Signature

interface FiberFailure extends Error, Inspectable {
readonly [FiberFailureCauseId]: Cause<unknown>;
readonly [FiberFailureId]: typeof FiberFailureId;
}
Since v2.0.0

Signature

interface RunCallbackOptions<in A, in E = never> extends RunForkOptions {
readonly onExit?: (exit: Exit<A, E>) => void;
}
Since v2.0.0

Signature

interface RunForkOptions {
readonly immediate?: boolean;
readonly scheduler?: Scheduler;
readonly scope?: Scope;
readonly updateRefs?: (refs: FiberRefs, fiberId: Runtime) => FiberRefs;
}

Runtime

interface
Since v2.0.0

Signature

interface Runtime<in R> extends Pipeable {
readonly context: Context<R>;
readonly fiberRefs: FiberRefs;
readonly runtimeFlags: RuntimeFlags;
}

Other

Since v3.12.0

Runtime Flags

Signature

declare const disableRuntimeFlag: {
(flag: RuntimeFlag): <R>(self: Runtime<R>) => Runtime<R>;
<R>(self: Runtime<R>, flag: RuntimeFlag): Runtime<R>;
}

Signature

declare const enableRuntimeFlag: {
(flag: RuntimeFlag): <R>(self: Runtime<R>) => Runtime<R>;
<R>(self: Runtime<R>, flag: RuntimeFlag): Runtime<R>;
}

Signature

declare const updateRuntimeFlags: {
(f: (flags: RuntimeFlags) => RuntimeFlags): <R>(self: Runtime<R>) => Runtime<R>;
<R>(self: Runtime<R>, f: (flags: RuntimeFlags) => RuntimeFlags): Runtime<R>;
}

Symbols

Signature

declare const FiberFailureCauseId: unique symbol

Signature

declare const FiberFailureId: typeof FiberFailureId
Since v2.0.0

Signature

type FiberFailureId = typeof FiberFailureId