Skip to main content Link Search Menu Expand Document (external link)

ManagedRuntime overview

Added in v2.0.0


Table of contents


guards

isManagedRuntime

Checks if the provided argument is a ManagedRuntime.

Signature

export declare const isManagedRuntime: (input: unknown) => input is ManagedRuntime<unknown, unknown>

Added in v3.9.0

models

ManagedRuntime (interface)

Signature

export interface ManagedRuntime<in R, out ER> extends Effect.Effect<Runtime.Runtime<R>, ER> {
  readonly [TypeId]: TypeId
  readonly memoMap: Layer.MemoMap
  readonly runtimeEffect: Effect.Effect<Runtime.Runtime<R>, ER>
  readonly runtime: () => Promise<Runtime.Runtime<R>>

  /**
   * Executes the effect using the provided Scheduler or using the global
   * Scheduler if not provided
   */
  readonly runFork: <A, E>(
    self: Effect.Effect<A, E, R>,
    options?: Runtime.RunForkOptions
  ) => Fiber.RuntimeFiber<A, E | ER>

  /**
   * Executes the effect synchronously returning the exit.
   *
   * This method is effectful and should only be invoked at the edges of your
   * program.
   */
  readonly runSyncExit: <A, E>(effect: Effect.Effect<A, E, R>) => Exit.Exit<A, ER | E>

  /**
   * 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.
   */
  readonly runSync: <A, E>(effect: Effect.Effect<A, E, R>) => A

  /**
   * 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.
   */
  readonly runCallback: <A, E>(
    effect: Effect.Effect<A, E, R>,
    options?: Runtime.RunCallbackOptions<A, E | ER> | undefined
  ) => Runtime.Cancel<A, E | ER>

  /**
   * 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.
   */
  readonly runPromise: <A, E>(
    effect: Effect.Effect<A, E, R>,
    options?: {
      readonly signal?: AbortSignal | undefined
    }
  ) => 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.
   */
  readonly runPromiseExit: <A, E>(
    effect: Effect.Effect<A, E, R>,
    options?: {
      readonly signal?: AbortSignal | undefined
    }
  ) => Promise<Exit.Exit<A, ER | E>>

  /**
   * Dispose of the resources associated with the runtime.
   */
  readonly dispose: () => Promise<void>

  /**
   * Dispose of the resources associated with the runtime.
   */
  readonly disposeEffect: Effect.Effect<void, never, never>

  readonly [Unify.typeSymbol]?: unknown
  readonly [Unify.unifySymbol]?: ManagedRuntimeUnify<this>
  readonly [Unify.ignoreSymbol]?: ManagedRuntimeUnifyIgnore
}

Added in v2.0.0

ManagedRuntimeUnify (interface)

Signature

export interface ManagedRuntimeUnify<A extends { [Unify.typeSymbol]?: any }> extends Effect.EffectUnify<A> {
  ManagedRuntime?: () => Extract<A[Unify.typeSymbol], ManagedRuntime<any, any>>
}

Added in v3.9.0

ManagedRuntimeUnifyIgnore (interface)

Signature

export interface ManagedRuntimeUnifyIgnore extends Effect.EffectUnifyIgnore {
  Effect?: true
}

Added in v3.9.0

runtime class

make

Convert a Layer into an ManagedRuntime, that can be used to run Effect’s using your services.

Signature

export declare const make: <R, E>(
  layer: Layer.Layer<R, E, never>,
  memoMap?: Layer.MemoMap | undefined
) => ManagedRuntime<R, E>

Example

import { Console, Effect, Layer, ManagedRuntime } from "effect"

class Notifications extends Effect.Tag("Notifications")<
  Notifications,
  { readonly notify: (message: string) => Effect.Effect<void> }
>() {
  static Live = Layer.succeed(this, { notify: (message) => Console.log(message) })
}

async function main() {
  const runtime = ManagedRuntime.make(Notifications.Live)
  await runtime.runPromise(Notifications.notify("Hello, world!"))
  await runtime.dispose()
}

main()

Added in v2.0.0

symbol

TypeId

Signature

export declare const TypeId: typeof TypeId

Added in v3.9.0

TypeId (type alias)

Signature

export type TypeId = typeof TypeId

Added in v3.9.0

utils

ManagedRuntime (namespace)

Added in v3.4.0

Context (type alias)

Signature

export type Context<T extends ManagedRuntime<any, any>> = [T] extends [ManagedRuntime<infer R, infer _E>] ? R : never

Added in v3.4.0

Error (type alias)

Signature

export type Error<T extends ManagedRuntime<any, any>> = [T] extends [ManagedRuntime<infer _R, infer E>] ? E : never

Added in v3.4.0