<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
>Creates an RcMap that can contain multiple reference counted resources that can be indexed
by a key. The resources are lazily acquired on the first call to get and
released when the last reference is released.
When to use
Use to create a scoped reference-counted map for resources that should be acquired once per key and shared while in use.
Details
Complex keys can extend Equal and Hash to allow lookups by value.
capacity: The maximum number of resources that can be held in the map.idleTimeToLive: When the reference count reaches zero, the resource will be released after this duration.
Example (Creating a reference-counted map)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
const map = yield* RcMap.make({
lookup: (key: string) =>
Effect.acquireRelease(
Effect.succeed(`acquired ${key}`),
() => Effect.log(`releasing ${key}`)
)
})
// Get "foo" from the map twice, which will only acquire it once.
// It will then be released once the scope closes.
yield* RcMap.get(map, "foo").pipe(
Effect.andThen(RcMap.get(map, "foo")),
Effect.scoped
)
})export const 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
>
}
Creates an RcMap that can contain multiple reference counted resources that can be indexed
by a key. The resources are lazily acquired on the first call to get and
released when the last reference is released.
When to use
Use to create a scoped reference-counted map for resources that should be
acquired once per key and shared while in use.
Details
Complex keys can extend Equal and Hash to allow lookups by value.
capacity: The maximum number of resources that can be held in the map.
idleTimeToLive: When the reference count reaches zero, the resource will be released after this duration.
Example (Creating a reference-counted map)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
const map = yield* RcMap.make({
lookup: (key: string) =>
Effect.acquireRelease(
Effect.succeed(`acquired ${key}`),
() => Effect.log(`releasing ${key}`)
)
})
// Get "foo" from the map twice, which will only acquire it once.
// It will then be released once the scope closes.
yield* RcMap.get(map, "foo").pipe(
Effect.andThen(RcMap.get(map, "foo")),
Effect.scoped
)
})
make: {
<function (type parameter) K in <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, function (type parameter) A in <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>
A, function (type parameter) E in <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>
E, function (type parameter) R in <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>
R>(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: undefined
}
options: {
readonly lookup: (key: K) => Effect.Effect<A, E, R>lookup: (key: Kkey: function (type parameter) K in <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) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <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>
A, function (type parameter) E in <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>
E, function (type parameter) R in <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>
R>
readonly idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive?: import DurationDuration.type Duration.Input = /*unresolved*/ anyInput | ((key: Kkey: function (type parameter) K in <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) => import DurationDuration.type Duration.Input = /*unresolved*/ anyInput) | undefined
readonly capacity?: undefinedcapacity?: undefined
}): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface RcMap<in out K, in out A, in out E = never>An RcMap is a reference-counted map data structure that manages the lifecycle
of resources indexed by keys. Resources are lazily acquired and automatically
released when no longer in use.
When to use
Use to share scoped resources by key while automatically releasing them after
their last active reference is gone.
Example (Inspecting a reference-counted map)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
// Create an RcMap that manages database connections
const dbConnectionMap = yield* RcMap.make({
lookup: (dbName: string) =>
Effect.acquireRelease(
Effect.succeed(`Connection to ${dbName}`),
(conn) => Effect.log(`Closing ${conn}`)
),
capacity: 10,
idleTimeToLive: "5 minutes"
})
// The RcMap interface provides access to:
// - lookup: Function to acquire resources
// - capacity: Maximum number of resources
// - idleTimeToLive: Time before idle resources are released
// - state: Current state of the map
console.log(`Capacity: ${dbConnectionMap.capacity}`)
}).pipe(Effect.scoped)
RcMap<function (type parameter) K in <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, function (type parameter) A in <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>
A, function (type parameter) E in <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>
E>, never, import ScopeScope.Scope | function (type parameter) R in <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>
R>
<function (type parameter) K in <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>
K, function (type parameter) A in <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>
A, function (type parameter) E in <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>
E, function (type parameter) R in <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>
R>(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity: number
}
options: {
readonly lookup: (key: K) => Effect.Effect<A, E, R>lookup: (key: Kkey: function (type parameter) K in <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>
K) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <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>
A, function (type parameter) E in <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>
E, function (type parameter) R in <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>
R>
readonly idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive?: import DurationDuration.type Duration.Input = /*unresolved*/ anyInput | ((key: Kkey: function (type parameter) K in <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>
K) => import DurationDuration.type Duration.Input = /*unresolved*/ anyInput) | undefined
readonly capacity: numbercapacity: number
}): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface RcMap<in out K, in out A, in out E = never>An RcMap is a reference-counted map data structure that manages the lifecycle
of resources indexed by keys. Resources are lazily acquired and automatically
released when no longer in use.
When to use
Use to share scoped resources by key while automatically releasing them after
their last active reference is gone.
Example (Inspecting a reference-counted map)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
// Create an RcMap that manages database connections
const dbConnectionMap = yield* RcMap.make({
lookup: (dbName: string) =>
Effect.acquireRelease(
Effect.succeed(`Connection to ${dbName}`),
(conn) => Effect.log(`Closing ${conn}`)
),
capacity: 10,
idleTimeToLive: "5 minutes"
})
// The RcMap interface provides access to:
// - lookup: Function to acquire resources
// - capacity: Maximum number of resources
// - idleTimeToLive: Time before idle resources are released
// - state: Current state of the map
console.log(`Capacity: ${dbConnectionMap.capacity}`)
}).pipe(Effect.scoped)
RcMap<function (type parameter) K in <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>
K, function (type parameter) A in <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>
A, function (type parameter) E in <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>
E | import CauseCause.type Cause.ExceededCapacityError = /*unresolved*/ anyExceededCapacityError>, never, import ScopeScope.Scope | function (type parameter) R in <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>
R>
} = <function (type parameter) K in <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 | undefined;
}): any
K, function (type parameter) A in <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 | undefined;
}): any
A, function (type parameter) E in <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 | undefined;
}): any
E, function (type parameter) R in <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 | undefined;
}): any
R>(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: number | undefined
}
options: {
readonly lookup: (key: K) => Effect.Effect<A, E, R>lookup: (key: Kkey: function (type parameter) K in <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 | undefined;
}): any
K) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <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 | undefined;
}): any
A, function (type parameter) E in <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 | undefined;
}): any
E, function (type parameter) R in <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 | undefined;
}): any
R>
readonly idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive?: import DurationDuration.type Duration.Input = /*unresolved*/ anyInput | ((key: Kkey: function (type parameter) K in <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 | undefined;
}): any
K) => import DurationDuration.type Duration.Input = /*unresolved*/ anyInput) | undefined
readonly capacity?: number | undefinedcapacity?: number | undefined
}) =>
import EffectEffect.withFiber<interface RcMap<in out K, in out A, in out E = never>An RcMap is a reference-counted map data structure that manages the lifecycle
of resources indexed by keys. Resources are lazily acquired and automatically
released when no longer in use.
When to use
Use to share scoped resources by key while automatically releasing them after
their last active reference is gone.
Example (Inspecting a reference-counted map)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
// Create an RcMap that manages database connections
const dbConnectionMap = yield* RcMap.make({
lookup: (dbName: string) =>
Effect.acquireRelease(
Effect.succeed(`Connection to ${dbName}`),
(conn) => Effect.log(`Closing ${conn}`)
),
capacity: 10,
idleTimeToLive: "5 minutes"
})
// The RcMap interface provides access to:
// - lookup: Function to acquire resources
// - capacity: Maximum number of resources
// - idleTimeToLive: Time before idle resources are released
// - state: Current state of the map
console.log(`Capacity: ${dbConnectionMap.capacity}`)
}).pipe(Effect.scoped)
RcMap<function (type parameter) K in <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 | undefined;
}): any
K, function (type parameter) A in <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 | undefined;
}): any
A, function (type parameter) E in <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 | undefined;
}): any
E>, never, function (type parameter) R in <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 | undefined;
}): any
R | import ScopeScope.Scope>((fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber) => {
const const context: Context.Context<
Scope.Scope | R
>
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 = fiber: Fiber.Fiber<unknown, unknown>(parameter) fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | 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; <…;
}
fiber.context as import ContextContext.type Context.Context = /*unresolved*/ anyContext<function (type parameter) R in <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 | undefined;
}): any
R | import ScopeScope.Scope>
const const scope: Scope.Scopeconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope = import ContextContext.get(const context: Context.Context<
Scope.Scope | R
>
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, 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)
const const self: RcMap<K, A, E>const self: {
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; <…;
}
self = const makeUnsafe: <K, A, E>(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, Scope.Scope>
readonly context: Context.Context<never>
readonly scope: Scope.Scope
readonly idleTimeToLive: (
key: K
) => Duration.Duration
readonly capacity: number
}) => RcMap<K, A, E>
makeUnsafe<function (type parameter) K in <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 | undefined;
}): any
K, function (type parameter) A in <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 | undefined;
}): any
A, function (type parameter) E in <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 | undefined;
}): any
E>({
lookup: (
key: K
) => Effect.Effect<A, E, Scope.Scope>
lookup: options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: number | undefined
}
options.lookup: (key: K) => Effect.Effect<A, E, R>lookup as any,
context: Context.Context<Scope.Scope | R>(property) 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(property) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope,
idleTimeToLive: (key: K) => Duration.DurationidleTimeToLive: typeof options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: number | undefined
}
options.idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive === "function"
? import flowflow(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: number | undefined
}
options.idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive, import DurationDuration.fromInputUnsafe)
: import constantconstant(import DurationDuration.fromInputUnsafe(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: number | undefined
}
options.idleTimeToLive?: | Duration.Input
| ((key: K) => Duration.Input)
| undefined
idleTimeToLive ?? import DurationDuration.zero)),
capacity: numbercapacity: var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(options: {
readonly lookup: (
key: K
) => Effect.Effect<A, E, R>
readonly idleTimeToLive?:
| Duration.Input
| ((key: K) => Duration.Input)
| undefined
readonly capacity?: number | undefined
}
options.capacity?: number | undefinedcapacity ?? var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.POSITIVE_INFINITY: numberA value greater than the largest number that can be represented in JavaScript.
JavaScript displays POSITIVE_INFINITY values as infinity.
POSITIVE_INFINITY, 0)
})
return import EffectEffect.as(
import ScopeScope.const addFinalizerExit: (
scope: Scope,
finalizer: (
exit: Exit<any, any>
) => Effect<unknown>
) => Effect<void>
Registers an exit-aware finalizer on a scope.
When to use
Use when cleanup needs to know whether the scope closed with success,
failure, or interruption.
Details
If the scope is open, the finalizer runs when the scope closes and receives
the scope's exit value. If the scope is already closed, the finalizer runs
immediately with the stored exit value.
Example (Adding an exit-aware finalizer)
import { Console, Effect, Exit, Scope } from "effect"
const withResource = Effect.gen(function*() {
const scope = yield* Scope.make()
// Add a finalizer for cleanup
yield* Scope.addFinalizerExit(
scope,
(exit) =>
Console.log(
`Cleaning up resource. Exit: ${
Exit.isSuccess(exit) ? "Success" : "Failure"
}`
)
)
// Use the resource
yield* Console.log("Using resource")
// Close the scope
yield* Scope.close(scope, Exit.void)
})
addFinalizerExit(const scope: Scope.Scopeconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, () => {
if (const self: RcMap<K, A, E>const self: {
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; <…;
}
self.RcMap<K, A, E>.state: State<K, A, E>state._tag: "Open" | "Closed"_tag === "Closed") {
return import EffectEffect.void
}
const const map: MutableHashMap.MutableHashMap<
K,
State.Entry<A, E>
>
const map: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
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;
}
map = const self: RcMap<K, A, E>const self: {
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; <…;
}
self.RcMap<K, A, E>.state: State<K, A, E>(property) RcMap<K, A, E>.state: {
_tag: "Open";
map: MutableHashMap.MutableHashMap<K, Entry<A, E>>;
}
state.State<K, A, E>.Open<K, A, E>.map: MutableHashMap.MutableHashMap<K, Entry<A, E>>(property) State<K, A, E>.Open<K, A, E>.map: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
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;
}
map
const self: RcMap<K, A, E>const self: {
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; <…;
}
self.RcMap<K, A, E>.state: State<K, A, E>state = { State<K, A, E>.Closed._tag: "Closed"_tag: "Closed" }
return import EffectEffect.forEach(
const map: MutableHashMap.MutableHashMap<
K,
State.Entry<A, E>
>
const map: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
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;
}
map,
([, entry: any(parameter) entry: {
deferred: Deferred.Deferred<A, E>;
scope: Scope.Closeable;
finalizer: Effect.Effect<void>;
idleTimeToLive: Duration.Duration;
fiber: Fiber.Fiber<void> | undefined;
expiresAt: number;
refCount: number;
}
entry]) => import EffectEffect.exit(import ScopeScope.const close: <A, E>(
self: Scope,
exit: Exit<A, E>
) => Effect<void>
Closes a scope and runs its registered finalizers.
When to use
Use to close a scope manually with a specific exit value.
Details
Finalizers run in the scope's configured order and receive the supplied
Exit.
Example (Running scope finalizers)
import { Console, Effect, Exit, Scope } from "effect"
const resourceManagement = Effect.gen(function*() {
const scope = yield* Scope.make("sequential")
// Add multiple finalizers
yield* Scope.addFinalizer(scope, Console.log("Close database connection"))
yield* Scope.addFinalizer(scope, Console.log("Close file handle"))
yield* Scope.addFinalizer(scope, Console.log("Release memory"))
// Do some work...
yield* Console.log("Performing operations...")
// Close scope - finalizers run in reverse order of registration
yield* Scope.close(scope, Exit.succeed("Success!"))
// Output: "Release memory", "Close file handle", "Close database connection"
})
close(entry: any(parameter) entry: {
deferred: Deferred.Deferred<A, E>;
scope: Scope.Closeable;
finalizer: Effect.Effect<void>;
idleTimeToLive: Duration.Duration;
fiber: Fiber.Fiber<void> | undefined;
expiresAt: number;
refCount: number;
}
entry.scope, import ExitExit.void))
).pipe(
import EffectEffect.tap(() =>
import EffectEffect.sync(() => {
import MutableHashMapMutableHashMap.clear(const map: MutableHashMap.MutableHashMap<
K,
State.Entry<A, E>
>
const map: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
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;
}
map)
})
)
)
}),
const self: RcMap<K, A, E>const self: {
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; <…;
}
self
)
})