variable / @typed/fx

Fx.switchMap

Maps each element of an Fx to a new Fx, and switches to the latest inner Fx.

When a new element is emitted, the previous inner Fx is cancelled.

Package version
2.0.0-beta.13
Category
Concurrent work
Since
1.0.0
Member of
Fx

Follow one execution

Maps each element of an Fx to a new Fx, and switches to the latest inner Fx. When a new element is emitted, the previous inner Fx is cancelled.

Fx timelineswitchMap interrupts the old inner exactly when its replacement arrivesswitchMap
OperatorswitchMap(preview)
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.

Source and current-inner failures are forwarded and their services remain typed. The returned Fx requires Scope, which owns the current inner fiber. Source completion waits for the latest inner. Replacing or interrupting the output closes obsolete inner work and runs its scoped finalizers before the replacement proceeds.

Source implementation · Learn the surrounding model

Compare all Fx timelines →

Import

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

Access Fx.switchMap 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 switchMap: FlatMapLike;

Why

switchMap models work whose result becomes obsolete when a newer source value arrives, such as suggestions for the current query. Cancellation is the policy: obsolete work is not allowed to finish in the background.

Switching, ordering, and cardinality

Every source value calls f. Before the new inner starts, the previous inner is interrupted and its interruption is awaited. Only the latest inner remains active; values it emitted before replacement remain visible, while later values from the replaced inner are suppressed. There is no result buffer.

Ownership and lifetime

Source and current-inner failures are forwarded and their services remain typed. The returned Fx requires Scope, which owns the current inner fiber. Source completion waits for the latest inner. Replacing or interrupting the output closes obsolete inner work and runs its scoped finalizers before the replacement proceeds.

Examples

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

const queries = Fx.mergeAll(
  Fx.at("typed", "0 millis"),
  Fx.at("typed fx", "5 millis")
)
const suggestions = Fx.switchMap(queries, (query) =>
  Fx.ensuring(Fx.at(query, "20 millis"), Effect.log(`${query} closed`))
)
Effect.runPromise(Effect.scoped(Fx.collectAll(suggestions))).then(console.log)
// logs cleanup for "typed" and resolves ["typed fx"]

Other public imports

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

Source