Hyperlinkv0.8.0-beta.28

Layer

Layer.fromBuildconsteffect/Layer.ts:327
<ROut, E, RIn>(
  build: (
    memoMap: MemoMap,
    scope: Scope.Scope
  ) => Effect<Context.Context<ROut>, E, RIn>
): Layer<ROut, E, RIn>

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")
    })
  )
)
constructors
Source effect/Layer.ts:32713 lines
export const fromBuild = <ROut, E, RIn>(
  build: (
    memoMap: MemoMap,
    scope: Scope.Scope
  ) => Effect<Context.Context<ROut>, E, RIn>
): Layer<ROut, E, RIn> =>
  fromBuildUnsafe((memoMap: MemoMap, scope: Scope.Scope) => {
    const layerScope = Scope.forkUnsafe(scope)
    return internalEffect.onExit(
      build(memoMap, layerScope),
      (exit) => exit._tag === "Failure" ? Scope.close(layerScope, exit) : internalEffect.void
    )
  })
Referenced by 6 symbols