<A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>Gets the value from an RcRef, acquiring it first if needed.
When to use
Use to borrow the current resource within a Scope, acquiring it first if
necessary.
Details
The reference count is incremented for the current Scope, and a release
finalizer is added to that scope. When the current scope closes, the
reference is released; the resource is closed when the final reference is
released, subject to any configured idle time-to-live.
Example (Sharing one acquired value)
import { Effect, RcRef } from "effect"
const program = Effect.gen(function*() {
// Create an RcRef with a resource
const ref = yield* RcRef.make({
acquire: Effect.acquireRelease(
Effect.succeed("shared resource"),
(resource) => Effect.log(`Releasing ${resource}`)
)
})
// Get the value from the RcRef
const value1 = yield* RcRef.get(ref)
const value2 = yield* RcRef.get(ref)
// Both values are the same instance
console.log(value1 === value2) // true
return value1
})export const const get: <A, E>(
self: RcRef<A, E>
) => Effect.Effect<A, E, Scope>
Gets the value from an RcRef, acquiring it first if needed.
When to use
Use to borrow the current resource within a Scope, acquiring it first if
necessary.
Details
The reference count is incremented for the current Scope, and a release
finalizer is added to that scope. When the current scope closes, the
reference is released; the resource is closed when the final reference is
released, subject to any configured idle time-to-live.
Example (Sharing one acquired value)
import { Effect, RcRef } from "effect"
const program = Effect.gen(function*() {
// Create an RcRef with a resource
const ref = yield* RcRef.make({
acquire: Effect.acquireRelease(
Effect.succeed("shared resource"),
(resource) => Effect.log(`Releasing ${resource}`)
)
})
// Get the value from the RcRef
const value1 = yield* RcRef.get(ref)
const value2 = yield* RcRef.get(ref)
// Both values are the same instance
console.log(value1 === value2) // true
return value1
})
get: <function (type parameter) A in <A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>A, function (type parameter) E in <A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>E>(self: RcRef<A, E>(parameter) self: {
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 RcRef<out A, out E = never>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]
})
Namespace containing type-level members associated with RcRef.
Example (Referencing namespace types)
import type { RcRef } from "effect"
// Use RcRef namespace types
type MyRcRef = RcRef.RcRef<string, Error>
type MyVariance = RcRef.RcRef.Variance<string, Error>
RcRef<function (type parameter) A in <A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>A, function (type parameter) E in <A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>E>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>A, function (type parameter) E in <A, E>(self: RcRef<A, E>): Effect.Effect<A, E, Scope>E, Scope> = import internalinternal.const get: anyget