Hyperlinkv0.8.0-beta.28

Layer

Layer.fromBuildMemoconsteffect/Layer.ts:371
<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, with automatic memoization.

Details

This is similar to fromBuild but provides automatic memoization of the layer construction. The layer will be memoized based on the provided MemoMap.

Example (Memoizing layer construction)

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

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

const databaseLayer = Layer.fromBuildMemo(() =>
  Effect.sync(() =>
    Context.make(Database, {
      query: (sql: string) => Effect.succeed("result")
    })
  )
)
constructors
Source effect/Layer.ts:3719 lines
export const fromBuildMemo = <ROut, E, RIn>(
  build: (
    memoMap: MemoMap,
    scope: Scope.Scope
  ) => Effect<Context.Context<ROut>, E, RIn>
): Layer<ROut, E, RIn> => {
  const self: Layer<ROut, E, RIn> = fromBuild((memoMap, scope) => memoMap.getOrElseMemoize(self, scope, build))
  return self
}
Referenced by 3 symbols