ManagedRuntime
Runs many effects against services built once from a Layer.
A ManagedRuntime builds the services from a layer, keeps those services available for repeated effect runs, and releases acquired resources when it is disposed. This module includes the runtime type, a constructor, a guard, and runners for connecting Effect programs to JavaScript entry points such as promises, callbacks, and synchronous code.
Constructors
Guards
isManagedRuntime
Checks whether the provided argument is a ManagedRuntime.
When to use
Use to narrow an unknown value before treating it as a ManagedRuntime.
Details
The guard checks the internal ManagedRuntime marker property. It does not build the layer or inspect the runtime's services.
Gotchas
Disposed runtimes still carry the marker, so this guard does not prove the runtime is still usable.
See
makefor creating managed runtimes this guard recognizes
Signature
declare function isManagedRuntime(input: unknown): input is ManagedRuntime<unknown, unknown>;Models
ManagedRuntime interface
A runtime built from a layer that can execute effects requiring that layer's services.
When to use
Use as the reusable runtime value returned by make when application entry points or integration code need to run many effects against the same layer-built services.
Details
The runtime builds and caches its service context and owns the scope for resources acquired by the layer.
Gotchas
Dispose the runtime with dispose or disposeEffect when it is no longer needed.
See
Signature
interface ManagedRuntime<in R, out ER> {
[key: number]: () => Promise<void>;
readonly "~effect/ManagedRuntime": "~effect/ManagedRuntime";
cachedContext: Context<R> | undefined;
readonly context: () => Promise<Context<R>>;
readonly contextEffect: Effect<Context<R>, ER>;
readonly dispose: () => Promise<void>;
readonly disposeEffect: Effect<void, never, never>;
readonly memoMap: MemoMap;
readonly runCallback: <A, E>(
effect: Effect<A, E, R>,
options?: RunOptions & {
readonly onExit: (exit: Exit<A, ER | E>) => void;
},
) => (interruptor?: number) => void;
readonly runFork: <A, E>(self: Effect<A, E, R>, options?: RunOptions) => Fiber<A, ER | E>;
readonly runPromise: <A, E>(effect: Effect<A, E, R>, options?: RunOptions) => Promise<A>;
readonly runPromiseExit: <A, E>(
effect: Effect<A, E, R>,
options?: RunOptions,
) => Promise<Exit<A, ER | E>>;
readonly runSync: <A, E>(effect: Effect<A, E, R>) => A;
readonly runSyncExit: <A, E>(effect: Effect<A, E, R>) => Exit<A, ER | E>;
readonly scope: Closeable;
}Other
ManagedRuntime
Type helpers associated with ManagedRuntime.
When to use
Use to reference type-level helpers for extracting managed runtime services and layer errors.
Creates a
ManagedRuntimefrom a layer.When to use
Use to create a reusable runtime from a
Layerfor application entry points or integration code that runs many effects without rebuilding services.Details
The layer is built lazily on first use and its context is cached for subsequent runs. Resources acquired by the layer are owned by the runtime and are released when
disposeordisposeEffectis run.options.memoMapcan be used to share layer memoization with other layer builds.Gotchas
Dispose the runtime when it is no longer needed. A runtime cannot be reused after disposal.
See
ManagedRuntimefor the returned runtime interfaceLayer.MemoMap for shared layer memoizationLayer.build for lower-level scoped layer construction