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

    Class Channel<T>

    An asynchronous channel for sending and receiving messages between tasks.

    Supports both rendezvous (capacity = 0) and buffered (capacity > 0) modes.

    const ch = new Channel(0); // Rendezvous channel

    // Sender task async function sender() { await ch.send(42); console.log("Message sent"); }

    // Receiver task async function receiver() { const msg = await ch.receive(); console.log("Received message:", msg); } // Start sender and receiver sender(); receiver();


    @example
    const ch = new Channel<string>(10); // Buffered channel with capacity 10

    // Sender task
    async function sender() {
    for (let i = 0; i < 20; i++) {
    await ch.send(`Message ${i}`);
    console.log(`Sent: Message ${i}`);
    }
    }

    // Receiver task
    async function receiver() {
    for (let i = 0; i < 20; i++) {
    const msg = await ch.receive();
    console.log(`Received: ${msg}`);
    }
    }

    // Start sender and receiver
    sender();
    receiver

    Type Parameters

    • T
    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Create a new Channel with the given capacity.

      A capacity of 0 creates a rendezvous channel where sends and receives must be paired. A capacity greater than 0 creates a buffered channel.

      Type Parameters

      • T

      Parameters

      • capacity: number = Infinity

        The channel capacity (0 for rendezvous, >0 for buffered).

      Returns Channel<T>

      const rendezvousChannel = new Channel<number>(0);
      const bufferedChannel = new Channel<string>(5);
      const ch = new Channel<number>(3); // Buffered channel with capacity 3
      await ch.send(1);
      await ch.send(2);
      await ch.send(3);
      // The next send will wait until a receive occurs
      const sendPromise = ch.send(4);
      const msg = await ch.receive(); // Receives 1
      await sendPromise; // Now the send of 4 completes
      const ch = new Channel<number>(0); // Rendezvous channel
      const sendPromise = ch.send(42); // This will wait for a receiver
      const msg = await ch.receive(); // Receives 42, unblocking the sender
      await sendPromise; // Now the send completes

      If capacity is negative.

    Accessors

    Methods

    • Close the channel. All pending and future receives will reject.

      If the channel is already closed, this is a no-op.

      Any pending producers/receivers will be rejected with ChannelClosedError.

      Returns void

    • Wait to receive a message from the channel.

      If multiple receivers are waiting, the oldest receiver will be served first (FIFO).

      Parameters

      • Optionalsignal: AbortSignal | null

        An optional AbortSignal to cancel the receive operation.

      Returns Promise<T>

      A promise that resolves to the received message.

      If the channel is closed.

    • Send a message through the channel.

      If the channel capacity is zero ("rendezvous" channel), this will wait for a receiver to be ready.

      If the capacity is greater than zero (buffered), this will buffer the message or wait if the buffer is full. If a receiver is waiting, the message is delivered directly to the receiver.

      Parameters

      • message: T

        The message to send.

      • Optionalsignal: AbortSignal | null

      Returns Promise<void>

      A promise that resolves when the message has been sent.

      If the channel is closed.