<A>(subscription: PubSub.Subscription<A>): Channel<A>Creates a channel from a PubSub subscription.
Example (Creating channels from subscriptions)
import { Channel, Data, Effect, PubSub } from "effect"
class SubscriptionError extends Data.TaggedError("SubscriptionError")<{
readonly reason: string
}> {}
const program = Effect.gen(function*() {
// Create a PubSub
const pubsub = yield* PubSub.bounded<string>(32)
// Create a subscription
const subscription = yield* PubSub.subscribe(pubsub)
// Publish some messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
yield* PubSub.publish(pubsub, "from")
yield* PubSub.publish(pubsub, "PubSub")
// Create a channel from the subscription
const channel = Channel.fromSubscription(subscription)
// The channel will receive all published messages
return channel
})
// Real-time notifications example
const notificationChannel = Effect.gen(function*() {
const eventBus = yield* PubSub.unbounded<{ type: string; payload: any }>()
const userSubscription = yield* PubSub.subscribe(eventBus)
return Channel.fromSubscription(userSubscription)
})export const const fromSubscription: <A>(
subscription: PubSub.Subscription<A>
) => Channel<A>
Creates a channel from a PubSub subscription.
Example (Creating channels from subscriptions)
import { Channel, Data, Effect, PubSub } from "effect"
class SubscriptionError extends Data.TaggedError("SubscriptionError")<{
readonly reason: string
}> {}
const program = Effect.gen(function*() {
// Create a PubSub
const pubsub = yield* PubSub.bounded<string>(32)
// Create a subscription
const subscription = yield* PubSub.subscribe(pubsub)
// Publish some messages
yield* PubSub.publish(pubsub, "Hello")
yield* PubSub.publish(pubsub, "World")
yield* PubSub.publish(pubsub, "from")
yield* PubSub.publish(pubsub, "PubSub")
// Create a channel from the subscription
const channel = Channel.fromSubscription(subscription)
// The channel will receive all published messages
return channel
})
// Real-time notifications example
const notificationChannel = Effect.gen(function*() {
const eventBus = yield* PubSub.unbounded<{ type: string; payload: any }>()
const userSubscription = yield* PubSub.subscribe(eventBus)
return Channel.fromSubscription(userSubscription)
})
fromSubscription = <function (type parameter) A in <A>(subscription: PubSub.Subscription<A>): Channel<A>A>(
subscription: PubSub.Subscription<A>(parameter) subscription: {
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; <…;
}
subscription: import PubSubPubSub.interface Subscription<out A>A subscription represents a consumer's connection to a PubSub, allowing them to take messages.
Example (Taking messages from a subscription)
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: PubSub.Subscription<string> = yield* PubSub.subscribe(
pubsub
)
yield* PubSub.publishAll(pubsub, ["msg1", "msg2", "msg3"])
// Take individual messages
const message = yield* PubSub.take(subscription)
console.log(message) // "msg1"
// Take multiple messages
const messages = yield* PubSub.takeUpTo(subscription, 1)
console.log(messages) // ["msg2"]
const allMessages = yield* PubSub.takeAll(subscription)
console.log(allMessages) // ["msg3"]
}))
})
Subscription<function (type parameter) A in <A>(subscription: PubSub.Subscription<A>): Channel<A>A>
): 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<function (type parameter) A in <A>(subscription: PubSub.Subscription<A>): Channel<A>A> => const fromPull: <
OutElem,
OutErr,
OutDone,
EX,
EnvX,
Env
>(
effect: Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
unknown,
unknown,
unknown,
Env | EnvX
>
Creates a Channel from an Effect that produces a Pull.
Example (Creating channels from pulls)
import { Channel, Effect } from "effect"
const channel = Channel.fromPull(
Effect.succeed(Effect.succeed(42))
)
fromPull(import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(import EffectEffect.const onInterrupt: {
<XE, XR>(
finalizer: (
interruptors: ReadonlySet<number>
) => Effect<void, XE, XR>
): <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E | XE, R | XR>
<A, E, R, XE, XR>(
self: Effect<A, E, R>,
finalizer: (
interruptors: ReadonlySet<number>
) => Effect<void, XE, XR>
): Effect<A, E | XE, R | XR>
}
onInterrupt(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(subscription: PubSub.Subscription<A>(parameter) subscription: {
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; <…;
}
subscription), () => import CauseCause.done())))