(scope: Scope.Scope): <RIn, E, ROut>(
self: Layer<ROut, E, RIn>
) => Effect<Context.Context<ROut>, E, RIn>
<RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<
Context.Context<ROut>,
E,
RIn
>Builds a layer using an explicit scope.
When to use
Use to control the lifetime of layer resources with a scope supplied by the caller.
Details
Resources created by the layer are released when the supplied scope is closed, unless a resource extends its own scope.
Example (Building a layer with an explicit scope)
import { Context, Effect, Layer, Scope } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
// Build a layer with explicit scope control
const program = Effect.gen(function*() {
const scope = yield* Effect.scope
const dbLayer = Layer.effect(Database, Effect.gen(function*() {
console.log("Initializing database...")
yield* Scope.addFinalizer(
scope,
Effect.sync(() => console.log("Database closed"))
)
return { query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`Result: ${sql}`)) }
}))
// Build with specific scope - resources tied to this scope
const context = yield* Layer.buildWithScope(dbLayer, scope)
const database = Context.get(context, Database)
return yield* database.query("SELECT * FROM users")
// Database will be closed when scope is closed
})export const const buildWithScope: {
(scope: Scope.Scope): <RIn, E, ROut>(
self: Layer<ROut, E, RIn>
) => Effect<Context.Context<ROut>, E, RIn>
<RIn, E, ROut>(
self: Layer<ROut, E, RIn>,
scope: Scope.Scope
): Effect<Context.Context<ROut>, E, RIn>
}
Builds a layer using an explicit scope.
When to use
Use to control the lifetime of layer resources with a scope supplied by the
caller.
Details
Resources created by the layer are released when the supplied scope is
closed, unless a resource extends its own scope.
Example (Building a layer with an explicit scope)
import { Context, Effect, Layer, Scope } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
// Build a layer with explicit scope control
const program = Effect.gen(function*() {
const scope = yield* Effect.scope
const dbLayer = Layer.effect(Database, Effect.gen(function*() {
console.log("Initializing database...")
yield* Scope.addFinalizer(
scope,
Effect.sync(() => console.log("Database closed"))
)
return { query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`Result: ${sql}`)) }
}))
// Build with specific scope - resources tied to this scope
const context = yield* Layer.buildWithScope(dbLayer, scope)
const database = Context.get(context, Database)
return yield* database.query("SELECT * FROM users")
// Database will be closed when scope is closed
})
buildWithScope: {
(scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope: import ScopeScope.Scope): <function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>RIn, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) ROut in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>ROut>(self: Layer<ROut, E, RIn>(parameter) 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 <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>ROut, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>RIn>) => import EffectEffect<import ContextContext.type Context.Context = /*unresolved*/ anyContext<function (type parameter) ROut in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>ROut>, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<Context.Context<ROut>, E, RIn>RIn>
<function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>RIn, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) ROut in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>ROut>(self: Layer<ROut, E, RIn>(parameter) 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 <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>ROut, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>RIn>, 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 <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>ROut>, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>RIn>
} = import dualdual(2, <function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>RIn, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) ROut in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>ROut>(
self: Layer<ROut, E, RIn>(parameter) 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 <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>ROut, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>RIn>,
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 <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>ROut>, function (type parameter) E in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>E, function (type parameter) RIn in <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope.Scope): Effect<Context.Context<ROut>, E, RIn>RIn> =>
import corecore.const withFiber: <
A,
E = never,
R = never
>(
evaluate: (
fiber: FiberImpl<unknown, unknown>
) => Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
withFiber((fiber: internalEffect.FiberImpl<
unknown,
unknown
>
(parameter) fiber: {
id: number;
interruptible: boolean;
currentOpCount: number;
currentLoopCount: number;
_stack: Array<Primitive>;
_observers: Array<(exit: Exit.Exit<A, E>) => void>;
_exit: Exit.Exit<A, E> | undefined;
_currentExit: Exit.Exit<A, E> | undefined;
_children: Set<FiberImpl<any, any>> | undefined;
_interruptedCause: Cause.Cause<never> | undefined;
_yielded: Exit.Exit<any, any> | (() => void) | undefined;
context: Context.Context<never>;
currentScheduler: Scheduler.Scheduler;
currentTracerContext: Tracer.Tracer["context"];
currentSpan: Tracer.AnySpan | undefined;
currentLogLevel: LogLevel.LogLevel;
minimumLogLevel: LogLevel.LogLevel;
currentStackFrame: StackFrame | undefined;
runtimeMetrics: Metric.FiberRuntimeMetricsService | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
_dispatcher: Scheduler.SchedulerDispatcher | undefined;
currentDispatcher: SchedulerDispatcher;
getRef: <X>(ref: Context.Reference<X>) => X;
addObserver: (cb: (exit: Exit.Exit<unknown, unknown>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit.Exit<unknown, unknown> | undefined;
evaluate: (effect: core.Primitive) => void;
runLoop: (effect: core.Primitive) => typeof core.Yield | Exit.Exit<unknown, unknown>;
getCont: <S extends core.contA | core.contE>(symbol: S) => (core.Primitive & Record<S, (value: any, fiber: internalEffect.FiberImpl) => core.Primitive>) | undefined;
yieldWith: (value: Exit.Exit<any, any> | (() => void)) => core.Yield;
children: () => Set<Fiber.Fiber<any, any>>;
pipe: () => unknown;
setContext: (context: Context.Context<never>) => void;
currentSpanLocal: Tracer.Span | undefined;
}
fiber) =>
const buildWithMemoMap: {
(memoMap: MemoMap, scope: Scope.Scope): <
RIn,
E,
ROut
>(
self: Layer<ROut, E, RIn>
) => Effect<Context.Context<ROut>, E, RIn>
<RIn, E, ROut>(
self: Layer<ROut, E, RIn>,
memoMap: MemoMap,
scope: Scope.Scope
): Effect<Context.Context<ROut>, E, RIn>
}
buildWithMemoMap(
self: Layer<ROut, E, RIn>(parameter) 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,
class CurrentMemoMapclass CurrentMemoMap {
key: Identifier;
Service: {
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>;
};
forkOrCreate: <Services>(self: Context.Context<Services>) => MemoMap;
of: (this: void, self: MemoMap) => MemoMap;
context: (self: MemoMap) => Context.Context<CurrentMemoMap>;
use: (f: (service: MemoMap) => Effect<A, E, R>) => Effect<A, E, CurrentMemoMap | R>;
useSync: (f: (service: MemoMap) => A) => Effect<A, never, CurrentMemoMap>;
Identifier: Identifier;
stack: string | undefined;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
Context service for the current MemoMap used in layer construction.
When to use
Use when building custom layer operations that need to access the current
memoization map from the fiber context.
Details
This service wraps a MemoMap as a Context.Service, making it available
for dependency injection during layer construction.
CurrentMemoMap.CurrentMemoMap.forkOrCreate<Services>(self: Context.Context<Services>): MemoMapforkOrCreate(fiber: internalEffect.FiberImpl<
unknown,
unknown
>
(parameter) fiber: {
id: number;
interruptible: boolean;
currentOpCount: number;
currentLoopCount: number;
_stack: Array<Primitive>;
_observers: Array<(exit: Exit.Exit<A, E>) => void>;
_exit: Exit.Exit<A, E> | undefined;
_currentExit: Exit.Exit<A, E> | undefined;
_children: Set<FiberImpl<any, any>> | undefined;
_interruptedCause: Cause.Cause<never> | undefined;
_yielded: Exit.Exit<any, any> | (() => void) | undefined;
context: Context.Context<never>;
currentScheduler: Scheduler.Scheduler;
currentTracerContext: Tracer.Tracer["context"];
currentSpan: Tracer.AnySpan | undefined;
currentLogLevel: LogLevel.LogLevel;
minimumLogLevel: LogLevel.LogLevel;
currentStackFrame: StackFrame | undefined;
runtimeMetrics: Metric.FiberRuntimeMetricsService | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
_dispatcher: Scheduler.SchedulerDispatcher | undefined;
currentDispatcher: SchedulerDispatcher;
getRef: <X>(ref: Context.Reference<X>) => X;
addObserver: (cb: (exit: Exit.Exit<unknown, unknown>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit.Exit<unknown, unknown> | undefined;
evaluate: (effect: core.Primitive) => void;
runLoop: (effect: core.Primitive) => typeof core.Yield | Exit.Exit<unknown, unknown>;
getCont: <S extends core.contA | core.contE>(symbol: S) => (core.Primitive & Record<S, (value: any, fiber: internalEffect.FiberImpl) => core.Primitive>) | undefined;
yieldWith: (value: Exit.Exit<any, any> | (() => void)) => core.Yield;
children: () => Set<Fiber.Fiber<any, any>>;
pipe: () => unknown;
setContext: (context: Context.Context<never>) => void;
currentSpanLocal: Tracer.Span | undefined;
}
fiber.FiberImpl<unknown, unknown>.context: Context.Context<never>(property) FiberImpl<unknown, unknown>.context: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
context),
scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope
)
))