variable / @typed/fx/RefSubject

mapEffect

Transforms a RefSubject, Computed, or Filtered using an Effectful function.

Package version
2.0.0-beta.13
Category
Derived queries
Since
1.0.0

Import

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

Signatures

export declare const mapEffect: {
    <T extends RefSubject.Any | Computed.Any | Filtered.Any, B, E2, R2>(f: (a: Success<T>) => Effect.Effect<B, E2, R2>): (ref: T) => T extends Filtered.Any ? Filtered<B, Error<T> | E2, Services<T> | R2> : Computed<B, Error<T> | E2, Services<T> | R2>;
    <A, E, R, B, E2, R2>(ref: RefSubject<A, E, R> | Computed<A, E, R>, f: (a: A) => Effect.Effect<B, E2, R2>): Computed<B, E | E2, R | R2>;
    <A, E, R, B, E2, R2>(ref: Filtered<A, E, R>, f: (a: A) => Effect.Effect<B, E2, R2>): Filtered<B, E | E2, R | R2>;
    <R0, E0, A, E, R, E2, R2, C, E3, R3>(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => Effect.Effect<C, E3, R3>): Computed<C, E0 | E | E2 | E3, R0 | R2 | R3 | Exclude<R, Scope.Scope>>;
};

Why

Applies one Effectful derivation to current reads and pushed versions. RefSubject and Computed inputs return Computed and add only E2; a Filtered input returns Filtered and preserves its conditional absence in addition to E2.

Ownership and lifetime

No callback runs until the result is read or observed. Computed results cannot become absent and do not add NoSuchElementError; only the Filtered overload can fail a current read for absence. Callback services and failures remain R2 and E2, and the observing Scope owns subscription cleanup.

Examples

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

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

  // Transform with an async operation
  const doubled = RefSubject.mapEffect(count, (n) =>
    Effect.succeed(n * 2)
  )

  const value = yield* doubled
  console.log(value) // 10

  // Update source
  yield* RefSubject.set(count, 7)

  // Computed automatically updates
  const newValue = yield* doubled
  console.log(newValue) // 14
})

Other public imports

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

Source