<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")
})
)
)export const 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 = <function (type parameter) ROut in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>ROut, function (type parameter) E in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>E, function (type parameter) RIn in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>RIn>(
build: (
memoMap: MemoMap,
scope: Scope.Scope
) => Effect<Context.Context<ROut>, E, RIn>
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: MemoMap,
scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope: import ScopeScope.Scope
) => import EffectEffect<import ContextContext.type Context.Context = /*unresolved*/ anyContext<function (type parameter) ROut in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>ROut>, function (type parameter) E in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>E, function (type parameter) RIn in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>RIn>
): 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) ROut in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>ROut, function (type parameter) E in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>E, function (type parameter) RIn in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>RIn> => {
const const self: Layer<ROut, E, RIn>const self: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: 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) ROut in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>ROut, function (type parameter) E in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>E, function (type parameter) RIn in <ROut, E, RIn>(build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>): Layer<ROut, E, RIn>RIn> = const fromBuild: <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.
Details
The function receives a MemoMap for memoization and a Scope for resource management.
A child scope is created, and if the build fails, the child scope is closed.
Example (Constructing a layer from a build function)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const databaseLayer = Layer.fromBuild(() =>
Effect.sync(() =>
Context.make(Database, {
query: (sql: string) => Effect.succeed("result")
})
)
)
fromBuild((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) => 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.MemoMap.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>getOrElseMemoize(const self: Layer<ROut, E, RIn>const self: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, build: (
memoMap: MemoMap,
scope: Scope.Scope
) => Effect<Context.Context<ROut>, E, RIn>
build))
return const self: Layer<ROut, E, RIn>const self: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<ROut>, E, RIn>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self
}