Skip to content

RcRef

Reference-counted handles for sharing one scoped resource across many scoped users. An RcRef<A, E> acquires the resource lazily the first time get needs it, reuses that value while it is borrowed or kept idle, and finalizes it when the final borrowing scope closes unless an idle timeout keeps it available. The module also provides invalidate for forcing the next get to acquire a fresh resource.

5 exports Added in v3.5.0 Source

Combinators

get

Added in v3.5.0 Source

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.

Signature

declare const get: <A, E>(self: RcRef<A, E>) => Effect.Effect<A, E, Scope>;

invalidate

Added in v3.19.6 Source

Invalidates the currently cached resource, if one has been acquired.

When to use

Use to force future RcRef.get calls to acquire a fresh resource when the currently cached resource should no longer be reused.

Details

After invalidation, the next get acquires a fresh resource.

Gotchas

Invalidation does not revoke resources already borrowed by active scopes; those remain usable until their scopes close.

See

  • get for acquiring the current cached resource or the fresh resource after invalidation

Signature

declare const invalidate: <A, E>(self: RcRef<A, E>) => Effect.Effect<void>;

Constructors

make

Added in v3.5.0 Source

Creates an RcRef from an acquire effect.

When to use

Use to create a lazily acquired, reference-counted resource from an acquire effect.

Details

The resource is acquired lazily on the first get and shared by subsequent gets while it remains cached. Each get adds a reference to the current Scope. When the last reference is released, the resource is closed immediately by default, or after idleTimeToLive when that option is provided.

Signature

declare const make: <A, E, R>(options: {
  readonly acquire: Effect.Effect<A, E, R>;
  readonly idleTimeToLive?: Duration.Input;
}) => Effect.Effect<RcRef<A, E>, never, R | Scope>;

Models

RcRef interface

Added in v3.5.0 Source

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.

Signature

interface RcRef<out A, out E = never> extends Pipeable {
  readonly "~effect/RcRef": Variance<A, E>;
}

Other

RcRef

Added in v3.5.0 Source

Namespace containing type-level members associated with RcRef.