<A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>Constructs a layer lazily using the specified factory.
Details
The factory is evaluated only when the suspended layer is first built, and the result is memoized with normal layer sharing semantics.
Example (Choosing a layer lazily)
import { Context, Layer } from "effect"
class Config extends Context.Service<Config, string>()("Config") {}
const useProd = true
const layer = Layer.suspend(() =>
useProd
? Layer.succeed(Config, "https://api.example.com")
: Layer.succeed(Config, "http://localhost:3000")
)export const const suspend: <A, E, R>(
evaluate: LazyArg<Layer<A, E, R>>
) => Layer<A, E, R>
Constructs a layer lazily using the specified factory.
Details
The factory is evaluated only when the suspended layer is first built, and
the result is memoized with normal layer sharing semantics.
Example (Choosing a layer lazily)
import { Context, Layer } from "effect"
class Config extends Context.Service<Config, string>()("Config") {}
const useProd = true
const layer = Layer.suspend(() =>
useProd
? Layer.succeed(Config, "https://api.example.com")
: Layer.succeed(Config, "http://localhost:3000")
)
suspend = <function (type parameter) A in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>A, function (type parameter) E in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>E, function (type parameter) R in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>R>(evaluate: LazyArg<Layer<A, E, R>>evaluate: import LazyArgLazyArg<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, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>A, function (type parameter) E in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>E, function (type parameter) R in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>R>>): 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, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>A, function (type parameter) E in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>E, function (type parameter) R in <A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>R> =>
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((memoMap: MemoMap(parameter) memoMap: {
get: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn> | undefined;
getOrElseMemoize: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope, build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>) => Effect<Context.Context<ROut>, E, RIn>;
}
memoMap, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope) => import internalEffectinternalEffect.const suspend: <A, E, R>(
evaluate: LazyArg<Effect.Effect<A, E, R>>
) => Effect.Effect<A, E, R>
suspend(() => evaluate: LazyArg<Layer<A, E, R>>evaluate().build(memoMap: MemoMap(parameter) memoMap: {
get: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn> | undefined;
getOrElseMemoize: <RIn, E, ROut>(layer: Layer<ROut, E, RIn>, scope: Scope.Scope, build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>) => Effect<Context.Context<ROut>, E, RIn>;
}
memoMap, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope)))