variable / @typed/fx/Fx

observe

Observes the values of an Fx stream using a callback function. The callback can return void or an Effect which will be executed for each value.

Package version
2.0.0-beta.7
Category
Running effects
Since
1.0.0

Follow one execution

Run the source and await observation Effects; the resulting Effect has no collected array.

Fx timelineobserve(source, save)observe
Operatorobserve(source, save)
Read this diagram

Follow each lane from left to right. Events stacked vertically share a tick; the green cursor marks the current time across every lane.

  • A value
    The text inside the pill is the emitted value.
  • Work starts
    The raised chevron starts an inner run (^ in the source).
  • The run returns
    The vertical bar ends this lane’s run.
  • A cause is delivered
    The exclamation mark belongs to this lane.
  • Work is interrupted
    The cross marks cancellation of this run.
  • Current time
    The line and diamond move together across all lanes.
  • Happening now
    A highlighted event is at the current tick.
  • Still ahead
    Muted, dashed values have not happened yet.
  • Time continues
    The lane’s arrow is not a return marker. An empty stretch can be quiet work that is still running.

Illustrated ticks start at 0. At 1×, one illustrated tick takes one second; captions specify real durations when timing matters. A cause or interruption belongs to its lane, and other work may continue. Scroll horizontally to inspect the rest of a long timeline.

Observation starts only when the returned Effect runs. That Effect owns the source run and completes when the source completes. Interruption interrupts the internal fiber and source cleanup. Callback invocation follows the producer’s delivery behavior; observe adds no buffer or concurrency of its own. A source cause or a callback failure fails the returned Effect as E | E2.

Source implementation · Learn the surrounding model

Compare all Fx timelines →

Import

import { observe } from "@typed/fx/Fx";

Signatures

export declare const observe: {
    <A, E2 = never, R2 = never>(f: (value: A) => void | Effect<unknown, E2, R2>): <E, R>(fx: Fx<A, E, R>) => Effect<unknown, E | E2, R | R2>;
    <A, E, R, E2 = never, R2 = never>(fx: Fx<A, E, R>, f: (value: A) => void | Effect<unknown, E2, R2>): Effect<unknown, E | E2, R | R2>;
};

Why

observe is the primary Effect boundary for performing work for each pushed value. A plain callback is lifted to Effect, while an Effect callback contributes its own typed failures and services to the result.

Ownership and lifetime

Observation starts only when the returned Effect runs. That Effect owns the source run and completes when the source completes. Interruption interrupts the internal fiber and source cleanup. Callback invocation follows the producer’s delivery behavior; observe adds no buffer or concurrency of its own. A source cause or a callback failure fails the returned Effect as E | E2.

Examples

import { Effect, Ref } from "effect"
import { fromIterable, observe } from "@typed/fx/Fx"

const program = Effect.gen(function* () {
  const total = yield* Ref.make(0)
  yield* observe(fromIterable([1, 2, 3]), (value) => Ref.update(total, (n) => n + value))
  return yield* Ref.get(total)
})

Other public imports

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

Source