Hyperlinkv0.8.0-beta.28

Layer

Layer.buildWithMemoMapconsteffect/Layer.ts:622
(memoMap: MemoMap, scope: Scope.Scope): <RIn, E, ROut>(
  self: Layer<ROut, E, RIn>
) => Effect<Context.Context<ROut>, E, RIn>
<RIn, E, ROut>(
  self: Layer<ROut, E, RIn>,
  memoMap: MemoMap,
  scope: Scope.Scope
): Effect<Context.Context<ROut>, E, RIn>

Builds a layer into an Effect value, using the specified MemoMap to memoize the layer construction.

Example (Building layers with an explicit memo map)

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

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

class Logger extends Context.Service<Logger, {
  readonly log: (msg: string) => Effect.Effect<void>
}>()("Logger") {}

// Build layers with explicit memoization control
const program = Effect.gen(function*() {
  const memoMap = yield* Layer.makeMemoMap
  const scope = yield* Effect.scope

  // Build database layer with memoization
  const dbLayer = Layer.succeed(Database, {
    query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result"))
  })
  const dbContext = yield* Layer.buildWithMemoMap(dbLayer, memoMap, scope)

  // Build logger layer with same memoization (reuses memo if same layer)
  const loggerLayer = Layer.succeed(Logger, {
    log: Effect.fn("Logger.log")((msg: string) => Effect.sync(() => console.log(msg)))
  })
  const loggerContext = yield* Layer.buildWithMemoMap(
    loggerLayer,
    memoMap,
    scope
  )

  return {
    database: Context.get(dbContext, Database),
    logger: Context.get(loggerContext, Logger)
  }
})
memo map
Source effect/Layer.ts:62220 lines
export const buildWithMemoMap: {
  (
    memoMap: MemoMap,
    scope: Scope.Scope
  ): <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Effect<Context.Context<ROut>, E, RIn>
  <RIn, E, ROut>(
    self: Layer<ROut, E, RIn>,
    memoMap: MemoMap,
    scope: Scope.Scope
  ): Effect<Context.Context<ROut>, E, RIn>
} = dual(3, <RIn, E, ROut>(
  self: Layer<ROut, E, RIn>,
  memoMap: MemoMap,
  scope: Scope.Scope
): Effect<Context.Context<ROut>, E, RIn> =>
  internalEffect.provideService(
    internalEffect.map(self.build(memoMap, scope), Context.add(CurrentMemoMap, memoMap)),
    CurrentMemoMap,
    memoMap
  ))
Referenced by 6 symbols