variable / @typed/fx

Push.switchMapEffect

Transforms each output value into an Effect, keeping only the latest Effect.

Each new outer value interrupts the previous Effect before starting its own. Every Effect can emit at most one value; interrupted Effects emit none. The input side is unchanged.

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

Import

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

Access Push.switchMapEffect through the imported Push 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 switchMapEffect: {
    <B, C, E3, R3>(f: (b: B) => Effect.Effect<C, E3, R3>): <A, E, R, E2, R2>(push: Push<A, E, R, B, E2, R2>) => Push<A, E, R, C, E2 | E3, R2 | R3 | Scope.Scope>;
    <A, E, R, B, E2, R2, C, E3, R3>(push: Push<A, E, R, B, E2, R2>, f: (b: B) => Effect.Effect<C, E3, R3>): Push<A, E, R, C, E2 | E3, R2 | R3 | Scope.Scope>;
};

Why

This is latest-wins Effect composition without manually wrapping each Effect as an Fx.

Ownership and lifetime

The output subscription’s Scope owns the active Effect fiber. Replacement and subscription interruption run its interruption/finalizers. E3 joins output errors and R3 joins required services.

Examples

import { Effect } from "effect"
import * as Fx from "@typed/fx/Fx"
import * as Push from "@typed/fx/Push"
import * as Sink from "@typed/fx/Sink"

const finalized: Array<number> = []
const source = Fx.make<number>((sink) =>
  sink.onSuccess(1).pipe(
    Effect.andThen(Effect.sleep("2 millis")),
    Effect.andThen(sink.onSuccess(2))
  )
)
const push = Push.make(Sink.make(() => Effect.void, (_: string) => Effect.void), source)
const latest = Push.switchMapEffect(push, (id) =>
  Effect.sleep(id === 1 ? "20 millis" : "1 millis").pipe(
    Effect.as(id),
    Effect.ensuring(Effect.sync(() => finalized.push(id)))
  )
)
const program = Fx.collectAll(latest).pipe(
  Effect.map((values) => ({ values, finalized })),
  Effect.scoped
)
// Effect.runPromise(program) => { values: [2], finalized: [1, 2] }

Other public imports

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

Source