variable / @typed/fx/RefSubject

modify

Modifies a RefSubject using a pure function that returns both a result and a new value.

Package version
2.0.0-beta.13
Category
Transactions
Since
1.0.0

Import

import { modify } from "@typed/fx/RefSubject";

Signatures

export declare const modify: {
    <A, B>(f: (value: A) => readonly [
        B,
        A
    ]): <E, R>(ref: RefSubject<A, E, R>) => Effect.Effect<B, E, R>;
    <A, E, R, B>(ref: RefSubject<A, E, R>, f: (value: A) => readonly [
        B,
        A
    ]): Effect.Effect<B, E, R>;
};

Why

Modifies a RefSubject using a pure function that returns both a result and a new value. The transition is serialized at the RefSubject rather than coordinated by callers or UI components.

Ownership and lifetime

Running modify performs one serialized subject transition and resolves with its committed value. It acquires no resource; failures and services remain those of the source ref.

Examples

import { Effect } from "effect"
import * as RefSubject from "@typed/fx/RefSubject"

const program = Effect.gen(function* () {
  const count = yield* RefSubject.make(5)

  // Get the old value and increment, returning the old value
  const oldValue = yield* RefSubject.modify(count, (value) => [value, value + 1] as const)

  console.log(oldValue) // 5
  const newValue = yield* count
  console.log(newValue) // 6
})

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source