<A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<
Arr.NonEmptyReadonlyArray<A>,
E,
Done
>Subscribes to a PubSub of Take values and exposes them as a channel.
Details
Output Take values are emitted as non-empty arrays. Failed Take values
fail the channel. Done Take values complete the channel.
export const const fromPubSubTake: <A, E, Done>(
pubsub: PubSub.PubSub<Take.Take<A, E, Done>>
) => Channel<
Arr.NonEmptyReadonlyArray<A>,
E,
Done
>
Subscribes to a PubSub of Take values and exposes them as a channel.
Details
Output Take values are emitted as non-empty arrays. Failed Take values
fail the channel. Done Take values complete the channel.
fromPubSubTake = <function (type parameter) A in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>A, function (type parameter) E in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>E, function (type parameter) Done in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>Done>(
pubsub: PubSub.PubSub<Take.Take<A, E, Done>>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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.interface PubSub<in out A>A PubSub<A> is an asynchronous message hub into which publishers can publish
messages of type A and subscribers can subscribe to take messages of type
A.
Example (Publishing and subscribing to messages)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
// Create a bounded PubSub with capacity 10
const pubsub = yield* PubSub.bounded<string>(10)
// Subscribe and consume messages
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
// Publish messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
const message1 = yield* PubSub.take(subscription)
const message2 = yield* PubSub.take(subscription)
console.log(message1, message2) // "Hello", "World"
}))
})
Companion namespace containing the low-level building blocks used by
PubSub, including atomic implementations, backing subscriptions, replay
windows, and delivery strategies.
PubSub<import TakeTake.type Take<A, E = never, Done = void> = anyRepresents one pull result: either a non-empty batch of values, a failure
Exit, or a successful Exit that signals completion with a Done value.
When to use
Use to store, transfer, or interpret pull results later while preserving
emitted values, failures, and normal completion.
Take<function (type parameter) A in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>A, function (type parameter) E in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>E, function (type parameter) Done in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>Done>>
): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<function (type parameter) A in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>A>, function (type parameter) E in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>E, function (type parameter) Done in <A, E, Done>(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>): Channel<Arr.NonEmptyReadonlyArray<A>, E, Done>Done> =>
const unwrap: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
R2,
E,
R
>(
channel: Effect.Effect<
Channel<
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
R2
>,
E,
R
>
) => Channel<
OutElem,
E | OutErr,
OutDone,
InElem,
InErr,
InDone,
Exclude<R, Scope.Scope> | R2
>
Constructs a Channel from a scoped effect that will result in a
Channel if successful.
Example (Unwrapping channel effects)
import { Channel, Data, Effect } from "effect"
class UnwrapError extends Data.TaggedError("UnwrapError")<{
readonly reason: string
}> {}
// Create an effect that produces a channel
const channelEffect = Effect.succeed(
Channel.fromIterable([1, 2, 3])
)
// Unwrap the effect to get the channel
const unwrappedChannel = Channel.unwrap(channelEffect)
// The resulting channel outputs: 1, 2, 3
unwrap(import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(import PubSubPubSub.const subscribe: <A>(
self: PubSub<A>
) => Effect.Effect<
Subscription<A>,
never,
Scope.Scope
>
Subscribes to receive messages from the PubSub. The resulting subscription can
be evaluated multiple times within the scope to take a message from the PubSub
each time.
Example (Subscribing to messages)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
const pubsub = yield* PubSub.bounded<string>(10)
// Subscribe within a scope for automatic cleanup
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
// Publish some messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
// Take messages one by one
const msg1 = yield* PubSub.take(subscription)
const msg2 = yield* PubSub.take(subscription)
console.log(msg1, msg2) // "Hello", "World"
// Subscription is automatically cleaned up when scope exits
}))
yield* Effect.scoped(Effect.gen(function*() {
const sub1 = yield* PubSub.subscribe(pubsub)
const sub2 = yield* PubSub.subscribe(pubsub)
// Multiple subscribers can receive the same messages
yield* PubSub.publish(pubsub, "Broadcast")
const [msg1, msg2] = yield* Effect.all([
PubSub.take(sub1),
PubSub.take(sub2)
])
console.log("Both received:", msg1, msg2) // "Broadcast", "Broadcast"
}))
})
subscribe(pubsub: PubSub.PubSub<Take.Take<A, E, Done>>(parameter) pubsub: {
pubsub: PubSub.Atomic<A>;
subscribers: PubSub.Subscribers<A>;
scope: Scope.Closeable;
shutdownHook: Latch.Latch;
shutdownFlag: MutableRef.MutableRef<boolean>;
strategy: PubSub.Strategy<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), (sub: PubSub.Subscription<
Take.Take<A, E, Done>
>
(parameter) sub: {
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; <…;
}
sub) => const fromEffectTake: <A, E, Done, E2, R>(
effect: Effect.Effect<
Take.Take<A, E, Done>,
E2,
R
>
) => Channel<
Arr.NonEmptyReadonlyArray<A>,
E | E2,
Done,
unknown,
unknown,
unknown,
R
>
Creates a channel from an effect that produces a Take.
Details
A successful Take emits a non-empty array of output elements. A failed
Take fails the channel. A done Take completes the channel with its done
value.
fromEffectTake(import PubSubPubSub.const take: <A>(
self: Subscription<A>
) => Effect.Effect<A>
Takes a single message from the subscription. If no messages are available,
this will suspend until a message becomes available.
Example (Taking a message)
import { Effect, Fiber, PubSub } from "effect"
const program = Effect.gen(function*() {
const pubsub = yield* PubSub.bounded<string>(10)
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
// Start a fiber to take a message (will suspend)
const takeFiber = yield* Effect.forkChild(
PubSub.take(subscription)
)
// Publish a message
yield* PubSub.publish(pubsub, "Hello")
// The take will now complete
const message = yield* Fiber.join(takeFiber)
console.log("Received:", message) // "Hello"
}))
})
take(sub: PubSub.Subscription<
Take.Take<A, E, Done>
>
(parameter) sub: {
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; <…;
}
sub))))