Hyperlinkv0.8.0-beta.28

PubSub

PubSub.sizeconsteffect/PubSub.ts:613
<A>(self: PubSub<A>): Effect.Effect<number>

Returns the current number of messages retained by the PubSub for active subscribers.

Details

If the PubSub has been shut down, the returned effect succeeds with 0. The size is not a count of waiting subscribers or suspended publishers.

Example (Getting PubSub size)

import { Effect, PubSub } from "effect"

const program = Effect.gen(function*() {
  const pubsub = yield* PubSub.bounded<string>(10)

  // Initially empty
  const initialSize = yield* PubSub.size(pubsub)
  console.log("Initial size:", initialSize) // 0

  yield* Effect.scoped(Effect.gen(function*() {
    const subscription = yield* PubSub.subscribe(pubsub)

    // Publish some messages for the active subscription
    yield* PubSub.publish(pubsub, "msg1")
    yield* PubSub.publish(pubsub, "msg2")

    const afterPublish = yield* PubSub.size(pubsub)
    console.log("After publishing:", afterPublish) // 2

    yield* PubSub.takeAll(subscription)
  }))
})
getters
Source effect/PubSub.ts:6131 lines
export const size = <A>(self: PubSub<A>): Effect.Effect<number> => Effect.sync(() => sizeUnsafe(self))
Referenced by 2 symbols