variable / @typed/fx

Fx.scanEffect

Scans the stream with an effectful function, emitting the accumulated state after each element. Emits the initial value first, then for each input a runs f(state, a) and emits the resulting state.

Package version
2.0.0-beta.7
Category
Stateful transforms
Since
1.0.0
Member of
Fx

Follow one execution

Scans the stream with an effectful function, emitting the accumulated state after each element. Emits the initial value first, then for each input a runs f(state, a) and emits the resulting state.

Fx timelinescanEffect emits each accumulated value when its reducer Effect resolvesscanEffect
OperatorscanEffect(100, oneTurnAdd)
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.

One mutable accumulator is owned per run. Reducer Effects inherit producer concurrency and use the captured services. Failure is delivered to the Sink without committing that transition; interruption follows the invoking callback rather than locking the entire run.

Source implementation · Learn the surrounding model

Compare all Fx timelines →

Import

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

Access Fx.scanEffect through the imported Fx 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 scanEffect: {
    <S, A, E2, R2>(initial: S, f: (s: S, a: A) => Effect.Effect<S, E2, R2>): <E, R>(fx: Fx<A, E, R>) => Fx<S, E | E2, R | R2>;
    <A, E, R, S, E2, R2>(fx: Fx<A, E, R>, initial: S, f: (s: S, a: A) => Effect.Effect<S, E2, R2>): Fx<S, E | E2, R | R2>;
};

Why

scanEffect permits a failing or service-backed accumulated transition and emits initial before subscribing. It does not serialize later producer callbacks: overlapping reductions can read the same state, finish out of order, and overwrite one another’s next state.

Ownership and lifetime

One mutable accumulator is owned per run. Reducer Effects inherit producer concurrency and use the captured services. Failure is delivered to the Sink without committing that transition; interruption follows the invoking callback rather than locking the entire run.

Examples

import { Effect } from "effect"
import { Fx } from "@typed/fx"

const concurrent = Fx.make<number>((sink) =>
  Effect.all([sink.onSuccess(1), sink.onSuccess(2)], { concurrency: "unbounded", discard: true })
)
const program = Fx.collectAll(concurrent.pipe(Fx.scanEffect(0, (sum, n) => Effect.succeed(sum + n))))
// Both overlapping reductions may observe sum === 0; serialize the producer for atomic totals.

Other public imports

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

Source