Hyperlinkv0.8.0-beta.28

Stream

Stream.interruptWhenconsteffect/Stream.ts:9713
<X, E2, R2>(effect: Effect.Effect<X, E2, R2>): <A, E, R>(
  self: Stream<A, E, R>
) => Stream<A, E2 | E, R2 | R>
<A, E, R, X, E2, R2>(
  self: Stream<A, E, R>,
  effect: Effect.Effect<X, E2, R2>
): Stream<A, E | E2, R | R2>

Interrupts the evaluation of this stream when the provided effect completes. The given effect will be forked as part of this stream, and its success will be discarded. This combinator will also interrupt any in-progress element being pulled from upstream.

Details

If the effect completes with a failure before the stream completes, the returned stream will emit that failure.

Example (Interrupting when an effect completes)

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

const program = Effect.gen(function*() {
  const interrupt = yield* Deferred.make<void>()
  const stream = Stream.make(1, 2, 3).pipe(
    Stream.tap((value) =>
      value === 2
        ? Deferred.succeed(interrupt, void 0)
        : Effect.void
    ),
    Stream.interruptWhen(Deferred.await(interrupt))
  )

  const result = yield* Stream.runCollect(stream)
  yield* Console.log(result)
})

Effect.runPromise(program)
// => [1, 2]
interruption
Source effect/Stream.ts:97138 lines
export const interruptWhen: {
  <X, E2, R2>(effect: Effect.Effect<X, E2, R2>): <A, E, R>(self: Stream<A, E, R>) => Stream<A, E2 | E, R2 | R>
  <A, E, R, X, E2, R2>(self: Stream<A, E, R>, effect: Effect.Effect<X, E2, R2>): Stream<A, E | E2, R | R2>
} = dual(
  2,
  <A, E, R, X, E2, R2>(self: Stream<A, E, R>, effect: Effect.Effect<X, E2, R2>): Stream<A, E | E2, R | R2> =>
    fromChannel(Channel.interruptWhen(self.channel, effect))
)