variable / @typed/fx

RefSubject.transform

Transforms a RefSubject invariantly using bidirectional mapping functions.

Package version
2.0.0-beta.13
Category
Writable views
Since
1.0.0
Member of
RefSubject

Import

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

Access RefSubject.transform through the imported RefSubject export. Its declaration below describes the member.

This public exposure is a re-export. Its import path is supported; declaration documentation is shared with the other public exposures below.

Signatures

export declare const transform: {
    <A, B>(toB: (a: A) => B, toA: (b: B) => A): <E, R>(ref: RefSubject<A, E, R>) => RefSubject<B, E, R>;
    <A, E, R, B>(ref: RefSubject<A, E, R>, toB: (a: A) => B, toA: (b: B) => A): RefSubject<B, E, R>;
};

Why

Creates a bidirectional lens over state: reads map outward and writes map back to the original RefSubject without allocating a second store.

Ownership and lifetime

The derived ref owns no independent state or subscription. It follows the source lifetime and routes writes through the source’s synchronized update boundary.

Examples

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

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

  // Transform to string and back
  const countStr = RefSubject.transform(
    count,
    (n) => n.toString(),
    (s) => parseInt(s, 10)
  )

  const value = yield* countStr
  console.log(value) // "5"

  // Set using the transformed type
  yield* RefSubject.set(countStr, "10")

  // Original reflects the change
  const original = yield* count
  console.log(original) // 10
})

Other public imports

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

Source