<
K,
L extends Layer.Layer<any, any, any>,
PreloadKeys extends Iterable<K> | undefined = undefined
>(
lookup: (key: K) => L,
options?:
| {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined
readonly preloadKeys?: PreloadKeys
}
| undefined
): Effect.Effect<
LayerMap<K, Layer.Success<L>, Layer.Error<L>>,
PreloadKeys extends undefined ? never : Layer.Error<L>,
Scope.Scope | Layer.Services<L>
>Creates a LayerMap that dynamically provides resources based on a key.
Example (Creating a layer map)
import { Context, Effect, Layer, LayerMap } from "effect"
// Define a service key
const DatabaseService = Context.Service<{
readonly query: (sql: string) => Effect.Effect<string>
}>("Database")
// Create a LayerMap that provides different database configurations
const program = Effect.gen(function*() {
const layerMap = yield* LayerMap.make(
(env: string) =>
Layer.succeed(DatabaseService)({
query: Effect.fn("DatabaseService.query")((sql) => Effect.succeed(`${env}: ${sql}`))
}),
{ idleTimeToLive: "5 seconds" }
)
// Get a layer for a specific environment
const devLayer = layerMap.get("development")
// Use the layer to provide the service
const result = yield* Effect.provide(
Effect.gen(function*() {
const db = yield* DatabaseService
return yield* db.query("SELECT * FROM users")
}),
devLayer
)
console.log(result) // "development: SELECT * FROM users"
})export const const make: <
K,
L extends Layer.Layer<any, any, any>,
PreloadKeys extends
| Iterable<K>
| undefined = undefined
>(
lookup: (key: K) => L,
options?:
| {
readonly idleTimeToLive?:
| IdleTimeToLiveInput<K>
| undefined
readonly preloadKeys?: PreloadKeys
}
| undefined
) => Effect.Effect<
LayerMap<K, Layer.Success<L>, Layer.Error<L>>,
PreloadKeys extends undefined
? never
: Layer.Error<L>,
Scope.Scope | Layer.Services<L>
>
Creates a LayerMap that dynamically provides resources based on a key.
Example (Creating a layer map)
import { Context, Effect, Layer, LayerMap } from "effect"
// Define a service key
const DatabaseService = Context.Service<{
readonly query: (sql: string) => Effect.Effect<string>
}>("Database")
// Create a LayerMap that provides different database configurations
const program = Effect.gen(function*() {
const layerMap = yield* LayerMap.make(
(env: string) =>
Layer.succeed(DatabaseService)({
query: Effect.fn("DatabaseService.query")((sql) => Effect.succeed(`${env}: ${sql}`))
}),
{ idleTimeToLive: "5 seconds" }
)
// Get a layer for a specific environment
const devLayer = layerMap.get("development")
// Use the layer to provide the service
const result = yield* Effect.provide(
Effect.gen(function*() {
const db = yield* DatabaseService
return yield* db.query("SELECT * FROM users")
}),
devLayer
)
console.log(result) // "development: SELECT * FROM users"
})
make: <
function (type parameter) K in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
K,
function (type parameter) L in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
L extends import LayerLayer.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<any, any, any>,
function (type parameter) PreloadKeys in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
PreloadKeys extends interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) K in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
K> | undefined = undefined
>(
lookup: (key: K) => Llookup: (key: Kkey: function (type parameter) K in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
K) => function (type parameter) L in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
L,
options: | {
readonly idleTimeToLive?:
| IdleTimeToLiveInput<K>
| undefined
readonly preloadKeys?: PreloadKeys
}
| undefined
options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefinedidleTimeToLive?: type IdleTimeToLiveInput<K> =
| Duration.Input
| ((key: K) => Duration.Input)
IdleTimeToLiveInput<function (type parameter) K in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
K> | undefined
readonly preloadKeys?: PreloadKeys | undefinedpreloadKeys?: function (type parameter) PreloadKeys in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
PreloadKeys
} | undefined
) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<
interface LayerMap<in out K, in out I, in out E = never>A scoped, keyed map of layer-built service contexts.
Details
A LayerMap builds resources for a key on demand, exposes them as a Layer
or scoped Context, and can invalidate cached resources for a key.
Example (Managing keyed layers)
import { Context, Effect, Layer, LayerMap } from "effect"
// Define a service key
const DatabaseService = Context.Service<{
readonly query: (sql: string) => Effect.Effect<string>
}>("Database")
// Create a LayerMap that provides different database configurations
const createDatabaseLayerMap = LayerMap.make((env: string) =>
Layer.succeed(DatabaseService)({
query: Effect.fn("DatabaseService.query")((sql) => Effect.succeed(`${env}: ${sql}`))
})
)
// Use the LayerMap
const program = Effect.gen(function*() {
const layerMap = yield* createDatabaseLayerMap
// Get a layer for a specific environment
const devLayer = layerMap.get("development")
// Get context directly
const context = yield* layerMap.contextEffect("production")
// Invalidate a cached layer
yield* layerMap.invalidate("development")
})
LayerMap<function (type parameter) K in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
K, import LayerLayer.type Success<T extends Layer.Any> =
T extends Layer.Layer<
infer _ROut,
infer _E,
infer _RIn
>
? _ROut
: never
Extracts the service output type (ROut) from a Layer type.
When to use
Use to derive the services provided by an existing or generic Layer without
restating its ROut type parameter.
Success<function (type parameter) L in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
L>, import LayerLayer.type Error<T extends Layer.Any> =
T extends Layer.Layer<
infer _ROut,
infer _E,
infer _RIn
>
? _E
: never
Extracts the error type (E) from a Layer type.
When to use
Use to derive a layer construction error type for helper types, wrappers, or
APIs that preserve a layer failure channel.
Error<function (type parameter) L in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
L>>,
function (type parameter) PreloadKeys in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
PreloadKeys extends undefined ? never : import LayerLayer.type Error<T extends Layer.Any> =
T extends Layer.Layer<
infer _ROut,
infer _E,
infer _RIn
>
? _E
: never
Extracts the error type (E) from a Layer type.
When to use
Use to derive a layer construction error type for helper types, wrappers, or
APIs that preserve a layer failure channel.
Error<function (type parameter) L in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
L>,
import ScopeScope.Scope | import LayerLayer.type Services<T extends Layer.Any> =
T extends infer L
? L extends Layer.Layer<
infer _ROut,
infer _E,
infer _RIn
>
? _RIn
: never
: never
Extracts the service requirements (RIn) from a Layer type.
When to use
Use to derive the dependency requirements of a generic or inferred Layer
without restating its RIn type parameter.
Services<function (type parameter) L in <K, L extends Layer.Layer<any, any, any>, PreloadKeys extends Iterable<K> | undefined = undefined>(lookup: (key: K) => L, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
readonly preloadKeys?: PreloadKeys;
} | undefined): Effect.Effect<LayerMap<K, Layer.Success<L>, Layer.Error<L>>, PreloadKeys extends undefined ? never : Layer.Error<L>, Scope.Scope | Layer.Services<L>>
L>
> = import EffectEffect.const fnUntraced: <Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<RcMap.RcMap<K, Context.Context<any>, any>, never, any>, LayerMap<K, any, any>, [lookup: (key: K) => Layer.Layer<any, any, any>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined]>(body: (this: unassigned, lookup: (key: K) => Layer.Layer<any, any, any>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined) => Generator<...>) => (lookup: (key: K) => Layer.Layer<any, any, any>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined) => Effect.Effect<...> (+41 overloads)
fnUntraced(function*<function (type parameter) I in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
I, function (type parameter) K in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
K, function (type parameter) EL in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
EL, function (type parameter) RL in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
RL>(
lookup: (key: K) => Layer.Layer<I, EL, RL>lookup: (key: Kkey: function (type parameter) K in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
K) => import LayerLayer.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) I in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
I, function (type parameter) EL in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
EL, function (type parameter) RL in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
RL>,
options: | {
readonly idleTimeToLive?:
| IdleTimeToLiveInput<K>
| undefined
}
| undefined
options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefinedidleTimeToLive?: type IdleTimeToLiveInput<K> =
| Duration.Input
| ((key: K) => Duration.Input)
IdleTimeToLiveInput<function (type parameter) K in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
K> | undefined
} | undefined
) {
const const context: Context.Context<never>const 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 = yield* import EffectEffect.const context: <R = never>() => Effect<
Context.Context<R>,
never,
R
>
Returns the complete context.
When to use
Use to read the complete Context available to the current effect.
Details
This function allows you to access all services that are currently available
in the effect's environment. This can be useful for debugging, introspection,
or when you need to pass the entire context to another function.
Example (Reading the full context)
import { Console, Context, Effect, Option } from "effect"
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
const Database = Context.Service<{
query: (sql: string) => string
}>("Database")
const program = Effect.gen(function*() {
const allServices = yield* Effect.context()
// Check if specific services are available
const loggerOption = Context.getOption(allServices, Logger)
const databaseOption = Context.getOption(allServices, Database)
yield* Console.log(`Logger available: ${Option.isSome(loggerOption)}`)
yield* Console.log(`Database available: ${Option.isSome(databaseOption)}`)
})
const context = Context.make(Logger, { log: console.log })
.pipe(Context.add(Database, { query: () => "result" }))
const provided = Effect.provideContext(program, context)
context<never>()
const const memoMap: Layer.MemoMapconst 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 = import LayerLayer.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: Layer.MemoMap) => Layer.MemoMap;
context: (self: Layer.MemoMap) => Context.Context<Layer.CurrentMemoMap>;
use: (f: (service: Layer.MemoMap) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, Layer.CurrentMemoMap | R>;
useSync: (f: (service: Layer.MemoMap) => A) => Effect.Effect<A, never, Layer.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(const context: Context.Context<never>const 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)
const const rcMap: RcMap.RcMap<
K,
Context.Context<I>,
EL
>
const rcMap: {
lookup: (key: K) => Effect.Effect<A, E, Scope.Scope>;
context: Context.Context<never>;
scope: Scope.Scope;
idleTimeToLive: (key: K) => Duration.Duration;
capacity: number;
state: State<K, A, E>;
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; <…;
}
rcMap = yield* import RcMapRcMap.const make: {
<K, A, E, R>(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: undefined
}): Effect.Effect<
RcMap<K, A, E>,
never,
Scope.Scope | R
>
<K, A, E, R>(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity: number
}): Effect.Effect<
RcMap<K, A, E | Cause.ExceededCapacityError>,
never,
Scope.Scope | R
>
}
make({
lookup: (
key: K
) => Effect.Effect<
Context.Context<I>,
EL,
Scope.Scope | RL
>
lookup: (key: Kkey: function (type parameter) K in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
K) =>
import EffectEffect.const contextWith: <R, A, E, R2>(
f: (
context: Context.Context<R>
) => Effect<A, E, R2>
) => Effect<A, E, R | R2>
Transforms the current context using the provided function.
When to use
Use to derive an effect from the complete Context.
Details
This function allows you to access the complete context and perform
computations based on all available services. This is useful when you need
to conditionally execute logic based on what services are available.
Example (Deriving values from the context)
import { Console, Context, Effect, Option } from "effect"
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
const Cache = Context.Service<{
get: (key: string) => string | null
}>("Cache")
const program = Effect.contextWith((services) => {
const cacheOption = Context.getOption(services, Cache)
const hasCache = Option.isSome(cacheOption)
if (hasCache) {
return Effect.gen(function*() {
const cache = yield* Effect.service(Cache)
yield* Console.log("Using cached data")
return cache.get("user:123") || "default"
})
} else {
return Effect.gen(function*() {
yield* Console.log("No cache available, using fallback")
return "fallback data"
})
}
})
const withCache = Effect.provideService(program, Cache, {
get: () => "cached_value"
})
contextWith((_: Context.Context<Scope.Scope>(parameter) _: {
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;
}
_: 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<import ScopeScope.Scope>) =>
import LayerLayer.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(lookup: (key: K) => Layer.Layer<I, EL, RL>lookup(key: Kkey), const memoMap: Layer.MemoMapconst 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, import ContextContext.const get: {
<Services, I extends Services, S>(
service: Key<I, S>
): (self: Context<Services>) => S
<Services, I extends Services, S>(
self: Context<Services>,
service: Key<I, S>
): S
}
get(_: Context.Context<Scope.Scope>(parameter) _: {
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;
}
_, import ScopeScope.const Scope: Context.Service<Scope, Scope>const Scope: {
key: string;
Service: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
};
of: (this: void, self: Scope.Scope) => Scope.Scope;
context: (self: Scope.Scope) => Context.Context<Scope.Scope>;
use: (f: (service: Scope.Scope) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, Scope.Scope | R>;
useSync: (f: (service: Scope.Scope) => A) => Effect.Effect<A, never, Scope.Scope>;
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;
}
A Scope represents a context where resources can be acquired and
automatically cleaned up when the scope is closed. Scopes can use
either sequential or parallel finalization strategies.
Example (Managing scoped resources)
import { Effect, Exit, Scope } from "effect"
const program = Effect.gen(function*() {
const scope = yield* Scope.make("sequential")
// Scope has a strategy and state
console.log(scope.strategy) // "sequential"
console.log(scope.state._tag) // "Open"
// Close the scope
yield* Scope.close(scope, Exit.void)
console.log(scope.state._tag) // "Closed"
})
Service tag for the active resource lifetime.
When to use
Use to access the active lifetime when registering finalizers or sharing
resources with the surrounding scope.
Example (Accessing the scope service)
import { Effect, Scope } from "effect"
const program = Effect.gen(function*() {
// Access the scope from the context
const scope = yield* Scope.Scope
// Use the scope for resource management
yield* Scope.addFinalizer(scope, Effect.log("Cleanup"))
})
// Provide a scope to the program
const scoped = Effect.scoped(program)
Scope))
),
idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive: options: | {
readonly idleTimeToLive?:
| IdleTimeToLiveInput<K>
| undefined
}
| undefined
options?.idleTimeToLive?: IdleTimeToLiveInput<K> | undefinedidleTimeToLive
})
return identity<LayerMap<K, I, any>>(a: LayerMap<K, I, any>): LayerMap<K, I, any>Returns its input argument unchanged.
When to use
Use to return a value unchanged where a function is required.
Example (Returning the same value)
import { identity } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(identity(5), 5)
identity<interface LayerMap<in out K, in out I, in out E = never>A scoped, keyed map of layer-built service contexts.
Details
A LayerMap builds resources for a key on demand, exposes them as a Layer
or scoped Context, and can invalidate cached resources for a key.
Example (Managing keyed layers)
import { Context, Effect, Layer, LayerMap } from "effect"
// Define a service key
const DatabaseService = Context.Service<{
readonly query: (sql: string) => Effect.Effect<string>
}>("Database")
// Create a LayerMap that provides different database configurations
const createDatabaseLayerMap = LayerMap.make((env: string) =>
Layer.succeed(DatabaseService)({
query: Effect.fn("DatabaseService.query")((sql) => Effect.succeed(`${env}: ${sql}`))
})
)
// Use the LayerMap
const program = Effect.gen(function*() {
const layerMap = yield* createDatabaseLayerMap
// Get a layer for a specific environment
const devLayer = layerMap.get("development")
// Get context directly
const context = yield* layerMap.contextEffect("production")
// Invalidate a cached layer
yield* layerMap.invalidate("development")
})
LayerMap<function (type parameter) K in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
K, function (type parameter) I in <I, K, EL, RL>(lookup: (key: K) => Layer.Layer<I, EL, RL>, options?: {
readonly idleTimeToLive?: IdleTimeToLiveInput<K> | undefined;
} | undefined): Generator<Effect.Effect<Context.Context<never>, never, never> | Effect.Effect<...>, LayerMap<...>, any>
I, any>>({
[const TypeId: "~effect/LayerMap"TypeId]: const TypeId: "~effect/LayerMap"TypeId,
LayerMap<K, I, any>.rcMap: RcMap.RcMap<K, Context.Context<I>, EL>(property) LayerMap<K, I, any>.rcMap: {
lookup: (key: K) => Effect.Effect<A, E, Scope.Scope>;
context: Context.Context<never>;
scope: Scope.Scope;
idleTimeToLive: (key: K) => Duration.Duration;
capacity: number;
state: State<K, A, E>;
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; <…;
}
The internal RcMap that stores the resources.
rcMap,
LayerMap<K, I, any>.get(key: K): Layer.Layer<I, any, never>Retrieves a Layer for the resources associated with the key.
get: (key: Kkey) => import LayerLayer.const effectContext: <A, E, R>(
effect: Effect<Context.Context<A>, E, R>
) => Layer<A, E, Exclude<R, Scope.Scope>>
Constructs a layer from an effect that produces all services in a Context.
When to use
Use when you need a Layer that effectfully constructs a Context with
multiple services.
Details
This allows you to create a Layer from an effectful computation that
returns multiple services. The Effect is executed in the scope of the
layer.
Example (Creating a layer from an effectful context)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<
Database,
{ readonly query: (sql: string) => Effect.Effect<string> }
>()("Database") {}
const layer = Layer.effectContext(
Effect.succeed(Context.make(Database, {
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
}))
)
effectContext(import RcMapRcMap.const get: {
<K>(key: K): <A, E>(
self: RcMap<K, A, E>
) => Effect.Effect<A, E, Scope.Scope>
<K, A, E>(
self: RcMap<K, A, E>,
key: K
): Effect.Effect<A, E, Scope.Scope>
}
get(const rcMap: RcMap.RcMap<
K,
Context.Context<I>,
EL
>
const rcMap: {
lookup: (key: K) => Effect.Effect<A, E, Scope.Scope>;
context: Context.Context<never>;
scope: Scope.Scope;
idleTimeToLive: (key: K) => Duration.Duration;
capacity: number;
state: State<K, A, E>;
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; <…;
}
rcMap, key: Kkey)),
LayerMap<K, I, any>.contextEffect(key: K): Effect.Effect<Context.Context<I>, any, Scope.Scope>Retrieves the context associated with the key.
contextEffect: (key: Kkey) => import RcMapRcMap.const get: {
<K>(key: K): <A, E>(
self: RcMap<K, A, E>
) => Effect.Effect<A, E, Scope.Scope>
<K, A, E>(
self: RcMap<K, A, E>,
key: K
): Effect.Effect<A, E, Scope.Scope>
}
get(const rcMap: RcMap.RcMap<
K,
Context.Context<I>,
EL
>
const rcMap: {
lookup: (key: K) => Effect.Effect<A, E, Scope.Scope>;
context: Context.Context<never>;
scope: Scope.Scope;
idleTimeToLive: (key: K) => Duration.Duration;
capacity: number;
state: State<K, A, E>;
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; <…;
}
rcMap, key: Kkey),
LayerMap<K, I, any>.invalidate(key: K): Effect.Effect<void>Invalidates the resource associated with the key.
invalidate: (key: Kkey) => import RcMapRcMap.const invalidate: {
<K>(key: K): <A, E>(
self: RcMap<K, A, E>
) => Effect.Effect<void>
<K, A, E>(
self: RcMap<K, A, E>,
key: K
): Effect.Effect<void>
}
invalidate(const rcMap: RcMap.RcMap<
K,
Context.Context<I>,
EL
>
const rcMap: {
lookup: (key: K) => Effect.Effect<A, E, Scope.Scope>;
context: Context.Context<never>;
scope: Scope.Scope;
idleTimeToLive: (key: K) => Duration.Duration;
capacity: number;
state: State<K, A, E>;
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; <…;
}
rcMap, key: Kkey)
})
})