Hyperlinkv0.8.0-beta.28

Stream

Stream.switchMapconsteffect/Stream.ts:2490
<A, A2, E2, R2>(
  f: (a: A) => Stream<A2, E2, R2>,
  options?:
    | {
        readonly concurrency?: number | "unbounded" | undefined
        readonly bufferSize?: number | undefined
      }
    | undefined
): <E, R>(self: Stream<A, E, R>) => Stream<A2, E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
  self: Stream<A, E, R>,
  f: (a: A) => Stream<A2, E2, R2>,
  options?:
    | {
        readonly concurrency?: number | "unbounded" | undefined
        readonly bufferSize?: number | undefined
      }
    | undefined
): Stream<A2, E | E2, R | R2>

Switches to the latest stream produced by the mapping function, interrupting the previous stream when a new element arrives.

Example (Switching to the latest stream)

import { Console, Effect, Stream } from "effect"

const program = Stream.make(1, 2, 3).pipe(
  Stream.switchMap((n) => (n === 3 ? Stream.make(n) : Stream.never)),
  Stream.runCollect
)

Effect.gen(function*() {
  const result = yield* program
  yield* Console.log(result)
  // Output: [ 3 ]
})
sequencing
Source effect/Stream.ts:249029 lines
export const switchMap: {
  <A, A2, E2, R2>(
    f: (a: A) => Stream<A2, E2, R2>,
    options?: {
      readonly concurrency?: number | "unbounded" | undefined
      readonly bufferSize?: number | undefined
    } | undefined
  ): <E, R>(self: Stream<A, E, R>) => Stream<A2, E2 | E, R2 | R>
  <A, E, R, A2, E2, R2>(
    self: Stream<A, E, R>,
    f: (a: A) => Stream<A2, E2, R2>,
    options?: {
      readonly concurrency?: number | "unbounded" | undefined
      readonly bufferSize?: number | undefined
    } | undefined
  ): Stream<A2, E | E2, R | R2>
} = dual((args) => isStream(args[0]), <A, E, R, A2, E2, R2>(
  self: Stream<A, E, R>,
  f: (a: A) => Stream<A2, E2, R2>,
  options?: {
    readonly concurrency?: number | "unbounded" | undefined
    readonly bufferSize?: number | undefined
  } | undefined
): Stream<A2, E | E2, R | R2> =>
  self.channel.pipe(
    Channel.flattenArray,
    Channel.switchMap((a) => f(a).channel, options),
    fromChannel
  ))