<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: import LazyArgLazyArg<import ContextContext.type Context.Context = /*unresolved*/ anyContext<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(import constantconstant(import internalEffectinternalEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(evaluate: LazyArg<Context.Context<A>>evaluate)))