/** * Stores a current value together with the scope that owns it. * * A `ScopedRef` is useful for resource-backed values such as clients, * connections, subscriptions, or handles. Replacing the value acquires the * replacement in a new scope and releases the resources owned by the previous * value. Reads can be effectful or synchronous, and updates are synchronized so * only one replacement happens at a time. * * @stability stable * @since 2.0.0 */ import * as Effect from "./Effect.ts"; import { type LazyArg } from "./Function.ts"; import type { Pipeable } from "./Pipeable.ts"; import * as Scope from "./Scope.ts"; import * as Synchronized from "./SynchronizedRef.ts"; declare const TypeId = "~effect/ScopedRef"; /** * A `ScopedRef` is a reference whose value is associated with resources, * which must be released properly. You can both get the current value of any * `ScopedRef`, as well as set it to a new value (which may require new * resources). The reference itself takes care of properly releasing resources * for the old value whenever a new value is obtained. * * **When to use** * * Use when an application needs to keep a current resource-backed value and * later replace it with another acquired value while ensuring the previous * value is released. * * @stability stable * @category models * @since 2.0.0 */ export interface ScopedRef extends Pipeable { readonly [TypeId]: typeof TypeId; readonly backing: Synchronized.SynchronizedRef; } /** * Creates a new `ScopedRef` from an effect that acquires the initial value. * * **When to use** * * Use when creating a `ScopedRef` whose initial value requires acquiring * resources that must be released. * * @see {@link make} for creating a `ScopedRef` from a value that does not require resource acquisition * @stability stable * @category constructors * @since 2.0.0 */ export declare const fromAcquire: (acquire: Effect.Effect) => Effect.Effect, E, Scope.Scope | R>; /** * Retrieves the current value of the scoped reference synchronously. * * **When to use** * * Use when you need immediate synchronous access to the current `ScopedRef` * value and can guarantee that reading outside the `Effect` API is safe. * * @see {@link get} for Effect-wrapped access in Effect programs * * @stability stable * @category getters * @since 4.0.0 */ export declare const getUnsafe: (self: ScopedRef) => A; /** * Retrieves the current value of the scoped reference effectfully. * * **When to use** * * Use to read the value currently stored in a `ScopedRef` inside an `Effect` * workflow. * * @see {@link getUnsafe} for reading the current value synchronously when an unsafe read is acceptable * * @stability stable * @category getters * @since 2.0.0 */ export declare const get: (self: ScopedRef) => Effect.Effect; /** * Creates a new `ScopedRef` from the specified value. * * **When to use** * * Use to create a `ScopedRef` when the initial value is already available or * can be produced without acquiring resources. * * **Details** * * The `evaluate` function runs when the returned effect runs. The returned * effect requires a `Scope`, and the reference closes the currently stored * value's scope when that outer scope closes. * * **Gotchas** * * Do not use `make` for an initial value whose creation acquires resources; use * `fromAcquire` so acquisition and finalization are tracked. * * @see {@link fromAcquire} for creating a `ScopedRef` from an effect that acquires the initial value * @see {@link set} for replacing the current value with a newly acquired value * * @stability stable * @category constructors * @since 2.0.0 */ export declare const make: (evaluate: LazyArg) => Effect.Effect, never, Scope.Scope>; /** * Sets the value of this reference to a newly acquired scoped value, releasing * any resources associated with the old value. * * **When to use** * * Use to replace the current value of an existing `ScopedRef` with a newly * acquired scoped value while releasing resources for the previous value. * * **Details** * * This method will not return until either the reference is successfully * changed to the new value, with old resources released, or until the attempt * to acquire a new value fails. * * @stability stable * @category mutations * @since 2.0.0 */ export declare const set: { /** * Sets the value of this reference to a newly acquired scoped value, releasing * any resources associated with the old value. * * **When to use** * * Use to replace the current value of an existing `ScopedRef` with a newly * acquired scoped value while releasing resources for the previous value. * * **Details** * * This method will not return until either the reference is successfully * changed to the new value, with old resources released, or until the attempt * to acquire a new value fails. * * @stability stable * @category mutations * @since 2.0.0 */ (acquire: Effect.Effect): (self: ScopedRef) => Effect.Effect>; /** * Sets the value of this reference to a newly acquired scoped value, releasing * any resources associated with the old value. * * **When to use** * * Use to replace the current value of an existing `ScopedRef` with a newly * acquired scoped value while releasing resources for the previous value. * * **Details** * * This method will not return until either the reference is successfully * changed to the new value, with old resources released, or until the attempt * to acquire a new value fails. * * @stability stable * @category mutations * @since 2.0.0 */ (self: ScopedRef, acquire: Effect.Effect): Effect.Effect>; }; export {}; //# sourceMappingURL=ScopedRef.d.ts.map