Package: effect
Module: RcRef
A reference counted reference that manages resource lifecycle.
When to use
Use to share a scoped resource across active users with reference-counted acquisition and release.
Details
An RcRef wraps a resource that can be acquired and released multiple times.
The resource is lazily acquired on the first call to get and automatically
released when the last reference is released.
Example (Sharing a lazily acquired resource)
import { Effect, RcRef } from "effect"
// Create an RcRef for a database connection
const createConnectionRef = (connectionString: string) =>
RcRef.make({
acquire: Effect.acquireRelease(
Effect.succeed(`Connected to ${connectionString}`),
(connection) => Effect.log(`Closing connection: ${connection}`)
)
})
// Use the RcRef in multiple operations
const program = Effect.gen(function*() {
const connectionRef = yield* createConnectionRef("postgres://localhost")
// Multiple gets will share the same connection
const connection1 = yield* RcRef.get(connectionRef)
const connection2 = yield* RcRef.get(connectionRef)
return [connection1, connection2]
})
Signature
export interface RcRef<out A, out E = never> extends Pipeable {
readonly [TypeId]: RcRef.Variance<A, E>
}
Since v3.5.0