<A>(pubsub: PubSub.Subscription<A>): Stream<A>Creates a stream from a PubSub subscription.
When to use
Use when you already have a PubSub.Subscription and want to expose its
messages as a Stream, with Stream.take or cancellation controlling how
many values are consumed.
Example (Creating a stream from a PubSub subscription)
import { Console, Effect, PubSub, Stream } from "effect"
const program = Effect.scoped(Effect.gen(function*() {
const pubsub = yield* PubSub.unbounded<number>()
const subscription = yield* PubSub.subscribe(pubsub)
yield* PubSub.publish(pubsub, 1)
yield* PubSub.publish(pubsub, 2)
const stream = Stream.fromSubscription(subscription)
const values = yield* stream.pipe(Stream.take(2), Stream.runCollect)
yield* Console.log(values)
}))
Effect.runPromise(program)
// Output: [ 1, 2 ]export const const fromSubscription: <A>(
pubsub: PubSub.Subscription<A>
) => Stream<A>
Creates a stream from a PubSub subscription.
When to use
Use when you already have a PubSub.Subscription and want to expose its
messages as a Stream, with Stream.take or cancellation controlling how
many values are consumed.
Example (Creating a stream from a PubSub subscription)
import { Console, Effect, PubSub, Stream } from "effect"
const program = Effect.scoped(Effect.gen(function*() {
const pubsub = yield* PubSub.unbounded<number>()
const subscription = yield* PubSub.subscribe(pubsub)
yield* PubSub.publish(pubsub, 1)
yield* PubSub.publish(pubsub, 2)
const stream = Stream.fromSubscription(subscription)
const values = yield* stream.pipe(Stream.take(2), Stream.runCollect)
yield* Console.log(values)
}))
Effect.runPromise(program)
// Output: [ 1, 2 ]
fromSubscription = <function (type parameter) A in <A>(pubsub: PubSub.Subscription<A>): Stream<A>A>(pubsub: PubSub.Subscription<A>(parameter) pubsub: {
pubsub: PubSub.Atomic<any>;
subscribers: PubSub.Subscribers<any>;
subscription: PubSub.BackingSubscription<A>;
pollers: MutableList.MutableList<Deferred.Deferred<any>>;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<any>;
replayWindow: PubSub.ReplayWindow<A>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
pubsub: import PubSubPubSub.type PubSub.Subscription = /*unresolved*/ anySubscription<function (type parameter) A in <A>(pubsub: PubSub.Subscription<A>): Stream<A>A>): interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<function (type parameter) A in <A>(pubsub: PubSub.Subscription<A>): Stream<A>A> =>
const fromChannel: <
Arr extends Arr.NonEmptyReadonlyArray<any>,
E,
R
>(
channel: Channel.Channel<
Arr,
E,
void,
unknown,
unknown,
unknown,
R
>
) => Stream<
Arr extends Arr.NonEmptyReadonlyArray<infer A>
? A
: never,
E,
R
>
Creates a stream from a array-emitting Channel.
Example (Creating a stream from an array-emitting channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const channel = Channel.succeed([1, 2, 3] as const)
const stream = Stream.fromChannel(channel)
const result = yield* Stream.runCollect(stream)
yield* Console.log(result)
})
// Output: [ 1, 2, 3 ]
fromChannel(import ChannelChannel.fromSubscriptionArray(pubsub: PubSub.Subscription<A>(parameter) pubsub: {
pubsub: PubSub.Atomic<any>;
subscribers: PubSub.Subscribers<any>;
subscription: PubSub.BackingSubscription<A>;
pollers: MutableList.MutableList<Deferred.Deferred<any>>;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<any>;
replayWindow: PubSub.ReplayWindow<A>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
pubsub))