<A>(evaluate: LazyArg<Context.Context<A>>): Layer<A>Constructs a layer lazily that provides all services in a Context.
When to use
Use when you need a Layer that creates multiple services synchronously but
defers that work until the layer is built.
Details
This is a lazy version of succeedContext where the Context is computed
synchronously only when the layer is built.
Example (Lazily providing a context)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.syncContext(() =>
Context.make(Database, {
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
})
)export const const syncContext: <A>(
evaluate: LazyArg<Context.Context<A>>
) => Layer<A>
Constructs a layer lazily that provides all services in a Context.
When to use
Use when you need a Layer that creates multiple services synchronously but
defers that work until the layer is built.
Details
This is a lazy version of succeedContext where the Context is computed
synchronously only when the layer is built.
Example (Lazily providing a context)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.syncContext(() =>
Context.make(Database, {
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
})
)
syncContext = <function (type parameter) A in <A>(evaluate: LazyArg<Context.Context<A>>): Layer<A>A>(evaluate: LazyArg<Context.Context<A>>evaluate: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) A in <A>(evaluate: LazyArg<Context.Context<A>>): Layer<A>A>>): interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) A in <A>(evaluate: LazyArg<Context.Context<A>>): Layer<A>A> =>
const fromBuildMemo: <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")
})
)
)
fromBuildMemo(constant<A>(value: A): LazyArg<A>Creates a zero-argument function that always returns the provided value.
When to use
Use when you need a thunk or callback that returns the same value on every
invocation.
Example (Creating a constant thunk)
import { Function } from "effect"
import * as assert from "node:assert"
const constNull = Function.constant(null)
assert.deepStrictEqual(constNull(), null)
assert.deepStrictEqual(constNull(), null)
constant(import internalEffectinternalEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(evaluate: LazyArg<Context.Context<A>>evaluate)))