Hyperlinkv0.8.0-beta.28

Ref

Ref.Refinterfaceeffect/Ref.ts:65
Ref<A>

A mutable reference that provides atomic read, write, and update operations.

When to use

Use to keep shared mutable state that is read and updated inside Effect programs.

Details

A Ref is a thread-safe mutable reference type for shared state. It supports simple read and write operations as well as atomic transformations.

Example (Reading and updating a ref)

import { Effect, Ref } from "effect"

const program = Effect.gen(function*() {
  // Create a ref with initial value
  const counter = yield* Ref.make(0)

  // Read the current value
  const value = yield* Ref.get(counter)
  console.log(value) // 0

  // Update the value atomically
  yield* Ref.update(counter, (n) => n + 1)

  // Read the updated value
  const newValue = yield* Ref.get(counter)
  console.log(newValue) // 1
})
Source effect/Ref.ts:6546 lines
export interface Ref<in out A> extends Ref.Variance<A>, Pipeable {
  readonly ref: MutableRef.MutableRef<A>
}

/**
 * The Ref namespace containing type definitions and utilities.
 *
 * **When to use**
 *
 * Use when referring to type members nested under the `Ref` namespace.
 *
 * @since 2.0.0
 */
export declare namespace Ref {
  /**
   * Variance interface for Ref types, defining the type parameter constraints.
   *
   * **When to use**
   *
   * Use when working with the type-level variance marker carried by `Ref`.
   *
   * **Example** (Using invariant refs)
   *
   * ```ts
   * import { Effect, Ref } from "effect"
   *
   * // This interface defines the invariant nature of Ref's type parameter
   * // A Ref<A> is both a producer and consumer of A
   * const program = Effect.gen(function*() {
   *   const ref = yield* Ref.make(42)
   *
   *   // Ref is invariant - it can both produce and consume numbers
   *   const value = yield* Ref.get(ref) // produces number
   *   yield* Ref.set(ref, value + 1) // consumes number
   * })
   * ```
   *
   * @category models
   * @since 2.0.0
   */
  export interface Variance<in out A> {
    readonly [TypeId]: {
      readonly _A: Invariant<A>
    }
  }
}
Referenced by 17 symbols