<A>(value: A): (self: PubSub<A>) => Effect.Effect<boolean>
<A>(self: PubSub<A>, value: A): Effect.Effect<boolean>Publishes a message to the PubSub as an Effect, returning whether the
message was accepted.
When to use
Use when you need to publish from effectful code and let the configured PubSub strategy handle surplus messages.
Details
The effect succeeds with false if the PubSub is shut down. If the message
cannot be accepted immediately, the configured strategy decides how surplus
messages are handled.
Example (Publishing a message)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
const pubsub = yield* PubSub.bounded<string>(10)
// Publish a message
const published = yield* PubSub.publish(pubsub, "Hello World")
console.log("Message published:", published) // true
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
yield* PubSub.publish(pubsub, "Hello")
const message = yield* PubSub.take(subscription)
console.log("Received:", message) // "Hello"
}))
})export const const publish: {
<A>(value: A): (
self: PubSub<A>
) => Effect.Effect<boolean>
<A>(
self: PubSub<A>,
value: A
): Effect.Effect<boolean>
}
Publishes a message to the PubSub as an Effect, returning whether the
message was accepted.
When to use
Use when you need to publish from effectful code and let the configured
PubSub strategy handle surplus messages.
Details
The effect succeeds with false if the PubSub is shut down. If the message
cannot be accepted immediately, the configured strategy decides how surplus
messages are handled.
Example (Publishing a message)
import { Effect, PubSub } from "effect"
const program = Effect.gen(function*() {
const pubsub = yield* PubSub.bounded<string>(10)
// Publish a message
const published = yield* PubSub.publish(pubsub, "Hello World")
console.log("Message published:", published) // true
yield* Effect.scoped(Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
yield* PubSub.publish(pubsub, "Hello")
const message = yield* PubSub.take(subscription)
console.log("Received:", message) // "Hello"
}))
})
publish: {
<function (type parameter) A in <A>(value: A): (self: PubSub<A>) => Effect.Effect<boolean>A>(value: Avalue: function (type parameter) A in <A>(value: A): (self: PubSub<A>) => Effect.Effect<boolean>A): (self: PubSub<A>(parameter) self: {
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; <…;
}
self: 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<function (type parameter) A in <A>(value: A): (self: PubSub<A>) => Effect.Effect<boolean>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<boolean>
<function (type parameter) A in <A>(self: PubSub<A>, value: A): Effect.Effect<boolean>A>(self: PubSub<A>(parameter) self: {
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; <…;
}
self: 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<function (type parameter) A in <A>(self: PubSub<A>, value: A): Effect.Effect<boolean>A>, value: Avalue: function (type parameter) A in <A>(self: PubSub<A>, value: A): Effect.Effect<boolean>A): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<boolean>
} = import dualdual(2, <function (type parameter) A in <A>(self: PubSub<A>, value: A): Effect.Effect<boolean>A>(self: PubSub<A>(parameter) self: {
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; <…;
}
self: 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<function (type parameter) A in <A>(self: PubSub<A>, value: A): Effect.Effect<boolean>A>, value: Avalue: function (type parameter) A in <A>(self: PubSub<A>, value: A): Effect.Effect<boolean>A): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<boolean> =>
import EffectEffect.suspend(() => {
if (self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<in out A>.shutdownFlag: MutableRef.MutableRef<boolean>(property) PubSub<in out A>.shutdownFlag: {
current: T;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
shutdownFlag.current) {
return import EffectEffect.succeed(false)
}
if (self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.pubsub: PubSub.Atomic<A>(property) PubSub<A>.pubsub: {
capacity: number;
isEmpty: () => boolean;
isFull: () => boolean;
size: () => number;
publish: (value: A) => boolean;
publishAll: (elements: Iterable<A>) => Array<A>;
slide: () => void;
subscribe: () => PubSub.BackingSubscription<A>;
replayWindow: () => PubSub.ReplayWindow<A>;
}
pubsub.PubSub<in out A>.Atomic<A>.publish(value: A): booleanpublish(value: Avalue)) {
self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.strategy: PubSub.Strategy<A>(property) PubSub<A>.strategy: {
shutdown: Effect.Effect<void>;
handleSurplus: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>, elements: Iterable<A>, isShutdown: MutableRef.MutableRef<boolean>) => Effect.Effect<boolean>;
onPubSubEmptySpaceUnsafe: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>) => void;
completePollersUnsafe: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>, subscription: PubSub.BackingSubscription<A>, pollers: MutableList.MutableList<Deferred.Deferred<A, never>>) => void;
completeSubscribersUnsafe: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>) => void;
}
strategy.PubSub<in out A>.Strategy<A>.completeSubscribersUnsafe(pubsub: PubSub<in out A>.Atomic<A>, subscribers: PubSub.Subscribers<A>): voidDescribes how publishers should signal to subscribers waiting for
additional values from the PubSub that new values are available.
completeSubscribersUnsafe(self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.pubsub: PubSub.Atomic<A>(property) PubSub<A>.pubsub: {
capacity: number;
isEmpty: () => boolean;
isFull: () => boolean;
size: () => number;
publish: (value: A) => boolean;
publishAll: (elements: Iterable<A>) => Array<A>;
slide: () => void;
subscribe: () => PubSub.BackingSubscription<A>;
replayWindow: () => PubSub.ReplayWindow<A>;
}
pubsub, self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.subscribers: PubSub.Subscribers<A>(property) PubSub<A>.subscribers: {
clear: () => void;
delete: (key: PubSub.BackingSubscription<A>) => boolean;
forEach: (callbackfn: (value: Set<MutableList.MutableList<Deferred.Deferred<A, never>>>, key: PubSub.BackingSubscription<A>, map: Map<PubSub.BackingSubscription<A>, Set<MutableList.MutableList<Deferred.Deferred<A, never>>>>) => void, thisArg?: any)…;
get: (key: PubSub.BackingSubscription<A>) => Set<MutableList.MutableList<Deferred.Deferred<A, never>>> | undefined;
has: (key: PubSub.BackingSubscription<A>) => boolean;
set: (key: PubSub.BackingSubscription<A>, value: Set<MutableList.MutableList<Deferred.Deferred<A, never>>>) => PubSub.Subscribers<A>;
size: number;
entries: () => MapIterator<[PubSub.BackingSubscription<A>, Set<MutableList.MutableList<Deferred.Deferred<A, never>>>]>;
keys: () => MapIterator<PubSub.BackingSubscription<A>>;
values: () => MapIterator<Set<MutableList.MutableList<Deferred.Deferred<A, never>>>>;
}
subscribers)
return import EffectEffect.succeed(true)
}
return self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.strategy: PubSub.Strategy<A>(property) PubSub<A>.strategy: {
shutdown: Effect.Effect<void>;
handleSurplus: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>, elements: Iterable<A>, isShutdown: MutableRef.MutableRef<boolean>) => Effect.Effect<boolean>;
onPubSubEmptySpaceUnsafe: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>) => void;
completePollersUnsafe: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>, subscription: PubSub.BackingSubscription<A>, pollers: MutableList.MutableList<Deferred.Deferred<A, never>>) => void;
completeSubscribersUnsafe: (pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>) => void;
}
strategy.function PubSub(pubsub: PubSub.Atomic<A>, subscribers: PubSub.Subscribers<A>, elements: Iterable<A>, isShutdown: MutableRef.MutableRef<boolean>): Effect.Effect<boolean>Describes how publishers should signal to subscribers that they are
waiting for space to become available in the PubSub.
handleSurplus(
self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.pubsub: PubSub.Atomic<A>(property) PubSub<A>.pubsub: {
capacity: number;
isEmpty: () => boolean;
isFull: () => boolean;
size: () => number;
publish: (value: A) => boolean;
publishAll: (elements: Iterable<A>) => Array<A>;
slide: () => void;
subscribe: () => PubSub.BackingSubscription<A>;
replayWindow: () => PubSub.ReplayWindow<A>;
}
pubsub,
self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<A>.subscribers: PubSub.Subscribers<A>(property) PubSub<A>.subscribers: {
clear: () => void;
delete: (key: PubSub.BackingSubscription<A>) => boolean;
forEach: (callbackfn: (value: Set<MutableList.MutableList<Deferred.Deferred<A, never>>>, key: PubSub.BackingSubscription<A>, map: Map<PubSub.BackingSubscription<A>, Set<MutableList.MutableList<Deferred.Deferred<A, never>>>>) => void, thisArg?: any)…;
get: (key: PubSub.BackingSubscription<A>) => Set<MutableList.MutableList<Deferred.Deferred<A, never>>> | undefined;
has: (key: PubSub.BackingSubscription<A>) => boolean;
set: (key: PubSub.BackingSubscription<A>, value: Set<MutableList.MutableList<Deferred.Deferred<A, never>>>) => PubSub.Subscribers<A>;
size: number;
entries: () => MapIterator<[PubSub.BackingSubscription<A>, Set<MutableList.MutableList<Deferred.Deferred<A, never>>>]>;
keys: () => MapIterator<PubSub.BackingSubscription<A>>;
values: () => MapIterator<Set<MutableList.MutableList<Deferred.Deferred<A, never>>>>;
}
subscribers,
[value: Avalue],
self: PubSub<A>(parameter) self: {
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; <…;
}
self.PubSub<in out A>.shutdownFlag: MutableRef.MutableRef<boolean>(property) PubSub<in out A>.shutdownFlag: {
current: T;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
shutdownFlag
)
}))