effect-io-ai

Package: effect
Module: Stream

Stream.fromPubSub

Creates a stream from a subscription to a PubSub.

Example (Creating a stream from a subscription to a PubSub)

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

const program = Effect.gen(function*() {
  const pubsub = yield* PubSub.unbounded<number>()

  const fiber = yield* Stream.fromPubSub(pubsub).pipe(
    Stream.take(3),
    Stream.runCollect,
    Effect.forkChild
  )

  yield* PubSub.publish(pubsub, 1)
  yield* PubSub.publish(pubsub, 2)
  yield* PubSub.publish(pubsub, 3)

  const values = yield* Fiber.join(fiber)
  yield* Console.log(values)
})

Effect.runPromise(program)
// Output: [ 1, 2, 3 ]

Signature

declare const fromPubSub: <A>(pubsub: PubSub.PubSub<A>) => Stream<A>

Source

Since v2.0.0