@ac-essentials/misc-util
    Preparing search index...

    Class Broadcast<T>

    A broadcast channel that allows multiple subscribers to receive messages.

    Each subscriber maintains its own message queue with a specified capacity. If a subscriber's queue is full when a new message is sent, the oldest message in that subscriber's queue is discarded to make room for the new message.

    const broadcast = new Broadcast<number>(5); // Capacity of 5 messages per subscriber

    // Subscriber 1
    const subscriber1 = broadcast.subscribe();
    function listen1() {
    while (!subscriber1.closed) {
    const msg = await subscriber1.receive();
    console.log("Subscriber 1 received:", msg);
    }
    }
    listen1();

    // Subscriber 2
    const subscriber2 = broadcast.subscribe();
    function listen2() {
    while (!subscriber2.closed) {
    const msg = await subscriber2.receive();
    console.log("Subscriber 2 received:", msg);
    }
    }
    listen2();

    // Send messages
    await broadcast.send(1);
    await broadcast.send(2);

    // Close the broadcast channel
    broadcast.close();

    Type Parameters

    • T
    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Creates a new Broadcast channel.

      All subscribers will have their own message queue with the specified capacity.

      Type Parameters

      • T

      Parameters

      • capacity: number

        The capacity of each subscriber's message queue.

      Returns Broadcast<T>

      If the capacity is less than or equal to zero.

    Accessors

    Methods

    • Sends a message to all subscribers.

      Parameters

      • message: T

        The message to send.

      • Optionalsignal: AbortSignal | null

        An optional AbortSignal to cancel the send operation.

      Returns Promise<void>

      A promise that resolves when the message has been sent to all subscribers.