<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"
}))
})subscriptions
Source effect/PubSub.ts:116019 lines
export const 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 = <function (type parameter) A in <A>(self: Subscription<A>): Effect.Effect<A>A>(self: Subscription<A>(parameter) self: {
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; <…;
}
self: 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>(self: Subscription<A>): Effect.Effect<A>A>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A>(self: Subscription<A>): Effect.Effect<A>A> =>
import EffectEffect.suspend(() => {
if (self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<out A>.shutdownFlag: MutableRef.MutableRef<boolean>(property) Subscription<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.interrupt
}
if (self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<A>.replayWindow: PubSub.ReplayWindow<A>(property) Subscription<A>.replayWindow: {
take: () => A | undefined;
takeN: (n: number) => Array<A>;
takeAll: () => Array<A>;
remaining: number;
}
replayWindow.PubSub<in out A>.ReplayWindow<A>.remaining: numberremaining > 0) {
const const message: NonNullable<A>message = self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<A>.replayWindow: PubSub.ReplayWindow<A>(property) Subscription<A>.replayWindow: {
take: () => A | undefined;
takeN: (n: number) => Array<A>;
takeAll: () => Array<A>;
remaining: number;
}
replayWindow.PubSub<in out A>.ReplayWindow<A>.take(): A | undefinedtake()!
return import EffectEffect.succeed(const message: NonNullable<A>message)
}
const const message: anymessage = self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<out A>.pollers: MutableList.MutableList<Deferred.Deferred<any>>(property) Subscription<out A>.pollers: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
pollers.length === 0
? self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<A>.subscription: PubSub.BackingSubscription<A>(property) Subscription<A>.subscription: {
isEmpty: () => boolean;
size: () => number;
poll: () => typeof MutableList.Empty | A;
pollUpTo: (n: number) => Array<A>;
unsubscribe: () => void;
}
subscription.PubSub<in out A>.BackingSubscription<A>.poll(): A | MutableList.Emptypoll()
: import MutableListMutableList.Empty
if (const message: anymessage === import MutableListMutableList.Empty) {
return const pollForItem: <A>(
self: Subscription<A>
) => Effect.Effect<A, never, never>
pollForItem(self: Subscription<A>(parameter) self: {
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; <…;
}
self)
} else {
self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<out A>.strategy: PubSub.Strategy<any>(property) Subscription<out A>.strategy: {
shutdown: Effect.Effect<void>;
handleSurplus: (pubsub: PubSub.Atomic<any>, subscribers: PubSub.Subscribers<any>, elements: Iterable<any>, isShutdown: MutableRef.MutableRef<boolean>) => Effect.Effect<boolean>;
onPubSubEmptySpaceUnsafe: (pubsub: PubSub.Atomic<any>, subscribers: PubSub.Subscribers<any>) => void;
completePollersUnsafe: (pubsub: PubSub.Atomic<any>, subscribers: PubSub.Subscribers<any>, subscription: PubSub.BackingSubscription<any>, pollers: MutableList.MutableList<Deferred.Deferred<any, never>>) => void;
completeSubscribersUnsafe: (pubsub: PubSub.Atomic<any>, subscribers: PubSub.Subscribers<any>) => void;
}
strategy.PubSub<in out A>.Strategy<any>.onPubSubEmptySpaceUnsafe(pubsub: PubSub<in out A>.Atomic<any>, subscribers: PubSub.Subscribers<any>): voidDescribes how subscribers should signal to publishers waiting for space
to become available in the PubSub that space may be available.
onPubSubEmptySpaceUnsafe(self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<out A>.pubsub: PubSub.Atomic<any>(property) Subscription<out A>.pubsub: {
capacity: number;
isEmpty: () => boolean;
isFull: () => boolean;
size: () => number;
publish: (value: any) => boolean;
publishAll: (elements: Iterable<any>) => Array<any>;
slide: () => void;
subscribe: () => PubSub.BackingSubscription<any>;
replayWindow: () => PubSub.ReplayWindow<any>;
}
pubsub, self: Subscription<A>(parameter) self: {
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; <…;
}
self.Subscription<out A>.subscribers: PubSub.Subscribers<any>(property) Subscription<out A>.subscribers: {
clear: () => void;
delete: (key: PubSub.BackingSubscription<any>) => boolean;
forEach: (callbackfn: (value: Set<MutableList.MutableList<Deferred.Deferred<any, never>>>, key: PubSub.BackingSubscription<any>, map: Map<PubSub.BackingSubscription<any>, Set<MutableList.MutableList<Deferred.Deferred<any, never>>>>) => void, thisAr…;
get: (key: PubSub.BackingSubscription<any>) => Set<MutableList.MutableList<Deferred.Deferred<any, never>>> | undefined;
has: (key: PubSub.BackingSubscription<any>) => boolean;
set: (key: PubSub.BackingSubscription<any>, value: Set<MutableList.MutableList<Deferred.Deferred<any, never>>>) => PubSub.Subscribers<any>;
size: number;
entries: () => MapIterator<[PubSub.BackingSubscription<any>, Set<MutableList.MutableList<Deferred.Deferred<any, never>>>]>;
keys: () => MapIterator<PubSub.BackingSubscription<any>>;
values: () => MapIterator<Set<MutableList.MutableList<Deferred.Deferred<any, never>>>>;
}
subscribers)
return import EffectEffect.succeed(const message: anymessage)
}
})Referenced by 3 symbols