effect-io-ai

Package: effect
Module: Layer

Layer.fromBuild

Constructs a Layer from a function that uses a MemoMap and Scope to build the layer.

Details

The function receives a MemoMap for memoization and a Scope for resource management. A child scope is created, and if the build fails, the child scope is closed.

Example (Constructing a layer from a build function)

import { Context, Effect, Layer } from "effect"

class Database extends Context.Service<Database, {
  readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}

const databaseLayer = Layer.fromBuild(() =>
  Effect.sync(() =>
    Context.make(Database, {
      query: (sql: string) => Effect.succeed("result")
    })
  )
)

Signature

declare const fromBuild: <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>) => Layer<ROut, E, RIn>

Source

Since v4.0.0