<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.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<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: <A, E>(
self_: RcRef<A, E>
) => Effect.Effect<A, E, Scope>
get