<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>Invalidates and removes a specific key from the RcMap. If the resource is not currently in use (reference count is 0), it will be immediately released.
When to use
Use to remove a resource by key so the next access performs a fresh lookup.
Example (Invalidating a resource)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
const map = yield* RcMap.make({
lookup: (key: string) =>
Effect.acquireRelease(
Effect.succeed(`Resource: ${key}`),
() => Effect.log(`Released ${key}`)
)
})
// Get a resource
yield* RcMap.get(map, "cache")
// Invalidate the resource - it will be removed from the map
// and released if no longer in use
yield* RcMap.invalidate(map, "cache")
// Next access will create a new resource
yield* RcMap.get(map, "cache")
}).pipe(Effect.scoped)export const 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>
}
Invalidates and removes a specific key from the RcMap. If the resource is not
currently in use (reference count is 0), it will be immediately released.
When to use
Use to remove a resource by key so the next access performs a fresh lookup.
Example (Invalidating a resource)
import { Effect, RcMap } from "effect"
Effect.gen(function*() {
const map = yield* RcMap.make({
lookup: (key: string) =>
Effect.acquireRelease(
Effect.succeed(`Resource: ${key}`),
() => Effect.log(`Released ${key}`)
)
})
// Get a resource
yield* RcMap.get(map, "cache")
// Invalidate the resource - it will be removed from the map
// and released if no longer in use
yield* RcMap.invalidate(map, "cache")
// Next access will create a new resource
yield* RcMap.get(map, "cache")
}).pipe(Effect.scoped)
invalidate: {
<function (type parameter) K in <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect.Effect<void>K>(key: Kkey: function (type parameter) K in <K>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect.Effect<void>K): <function (type parameter) A in <A, E>(self: RcMap<K, A, E>): Effect.Effect<void>A, function (type parameter) E in <A, E>(self: RcMap<K, A, E>): Effect.Effect<void>E>(self: RcMap<K, A, E>(parameter) 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: 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>(key: K): <A, E>(self: RcMap<K, A, E>) => Effect.Effect<void>K, function (type parameter) A in <A, E>(self: RcMap<K, A, E>): Effect.Effect<void>A, function (type parameter) E in <A, E>(self: RcMap<K, A, E>): Effect.Effect<void>E>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<void>
<function (type parameter) K in <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>K, function (type parameter) A in <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>A, function (type parameter) E in <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>E>(self: RcMap<K, A, E>(parameter) 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: 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>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>K, function (type parameter) A in <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>A, function (type parameter) E in <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>E>, key: Kkey: function (type parameter) K in <K, A, E>(self: RcMap<K, A, E>, key: K): Effect.Effect<void>K): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<void>
} = import dualdual(
2,
import EffectEffect.fnUntraced(function*<function (type parameter) K in <K, A, E>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>K, function (type parameter) A in <K, A, E>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>A, function (type parameter) E in <K, A, E>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>E>(self: RcMap<K, A, E>(parameter) 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: 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>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>K, function (type parameter) A in <K, A, E>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>A, function (type parameter) E in <K, A, E>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>E>, key: Kkey: function (type parameter) K in <K, A, E>(self: RcMap<K, A, E>, key: K): Generator<any, void, any>K) {
if (self: RcMap<K, A, E>(parameter) 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
const const o: Option<State.Entry<A, E>>o = import MutableHashMapMutableHashMap.get(self: RcMap<K, A, E>(parameter) 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, key: Kkey)
if (const o: Option<State.Entry<A, E>>o._tag === "None") return
const const entry: anyconst 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 = const o: Some<State.Entry<A, E>>const o: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
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;
}
o.value
import MutableHashMapMutableHashMap.remove(self: RcMap<K, A, E>(parameter) 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, key: Kkey)
if (const entry: anyconst 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.refCount > 0) return
if (const entry: anyconst 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.fiber) yield* import FiberFiber.interrupt(const entry: anyconst 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.fiber)
yield* 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(const entry: anyconst 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)
}, import EffectEffect.uninterruptible)
)