happy-rusty
    Preparing search index...

    Interface Channel<T>

    An MPMC (multi-producer multi-consumer) channel for async message passing.

    Channels allow multiple async tasks to communicate by sending and receiving typed values. Values are delivered in FIFO order.

    1.8.0

    // Create a bounded channel with capacity 10
    const channel = Channel<string>(10);

    // Producer
    await channel.send('hello');
    await channel.send('world');
    channel.close();

    // Consumer
    for await (const msg of channel) {
    console.log(msg);
    }
    // Rendezvous channel (direct handoff)
    const channel = Channel<number>(0);

    // This will block until someone receives
    const sendPromise = channel.send(42);

    // This unblocks the sender
    const value = await channel.receive(); // Some(42)
    await sendPromise; // Now completes
    interface Channel<T> {
        "[toStringTag]": "Channel";
        capacity: number;
        isClosed: boolean;
        isEmpty: boolean;
        isFull: boolean;
        length: number;
        receiver: Receiver<T>;
        sender: Sender<T>;
        "[asyncIterator]"(): AsyncIterator<T>;
        close(): void;
        receive(): AsyncOption<T>;
        receiveTimeout(ms: number): AsyncOption<T>;
        send(value: T): Promise<boolean>;
        sendTimeout(value: T, ms: number): Promise<boolean>;
        toString(): string;
        tryReceive(): Option<T>;
        trySend(value: T): boolean;
    }

    Type Parameters

    • T

      The type of values that can be sent through the channel.

    Index

    Properties

    "[toStringTag]": "Channel"

    The well-known symbol Symbol.toStringTag used by Object.prototype.toString(). Returns 'Channel' so that Object.prototype.toString.call(channel) produces '[object Channel]'.

    capacity: number

    The maximum number of values that can be buffered. 0 for rendezvous channels, Infinity for unbounded channels.

    const rendezvous = Channel<number>(0);
    console.log(rendezvous.capacity); // 0

    const unbounded = Channel<number>();
    console.log(unbounded.capacity); // Infinity
    isClosed: boolean

    Returns true if the channel has been closed.

    isEmpty: boolean

    Returns true if the channel buffer is empty. Note: A rendezvous channel (capacity=0) is always empty.

    const rendezvous = Channel<number>(0);
    console.log(rendezvous.isEmpty); // true

    const bounded = Channel<number>(1);
    console.log(bounded.isEmpty); // true
    await bounded.send(1);
    console.log(bounded.isEmpty); // false
    isFull: boolean

    Returns true if the channel buffer is full. Note: A rendezvous channel (capacity=0) is always full.

    const rendezvous = Channel<number>(0);
    console.log(rendezvous.isFull); // true

    const bounded = Channel<number>(1);
    console.log(bounded.isFull); // false
    await bounded.send(1);
    console.log(bounded.isFull); // true
    length: number

    The current number of values in the buffer.

    Note: This does not count waiting senders/receivers; it only counts buffered items.

    const ch = Channel<number>(0);
    const sendP = ch.send(1);
    const recvP = ch.receive();

    // Rendezvous channels don't buffer values.
    console.log(ch.length); // 0

    await recvP;
    await sendP;
    receiver: Receiver<T>

    A receiver-only view of this channel.

    The Receiver shares state with this channel but only exposes receive-related methods. Useful for type-safe producer/consumer separation.

    const ch = Channel<number>(10);
    const receiver = ch.receiver;

    // Pass to consumer - they can only receive
    for await (const msg of receiver) {
    console.log(msg);
    }
    // receiver.send(1) // Type error!
    sender: Sender<T>

    A sender-only view of this channel.

    The Sender shares state with this channel but only exposes send-related methods. Useful for type-safe producer/consumer separation.

    const ch = Channel<number>(10);
    const sender = ch.sender;

    // Pass to producer - they can only send
    await sender.send(42);
    // sender.receive() // Type error!

    Methods

    • Returns an async iterator that yields values until the channel is closed.

      Returns AsyncIterator<T>

      for await (const msg of channel) {
      console.log('Message:', msg);
      }
    • Closes the channel.

      After closing:

      • send() and trySend() will return false.
      • Pending senders will receive false.
      • receive() will drain remaining buffered values, then return None.
      • Pending receivers will receive None after the buffer is drained.

      Closing is idempotent - calling close() multiple times has no effect.

      Returns void

      const ch = Channel<number>(10);
      ch.send(1);
      ch.send(2);
      ch.close();

      // Can still receive buffered values
      await ch.receive(); // Some(1)
      await ch.receive(); // Some(2)
      await ch.receive(); // None
    • Receives a value from the channel, waiting if necessary.

      • If the buffer has values, returns Some(value) immediately.
      • If senders are waiting (rendezvous), receives directly from one.
      • If the buffer is empty and not closed, waits for a value.
      • If the channel is closed and empty, returns None.

      Returns AsyncOption<T>

      A promise that resolves to Some(value) or None if closed and empty.

      const ch = Channel<number>(10);
      void ch.send(1);

      const v1 = await ch.receive(); // Some(1)
      ch.close();
      const v2 = await ch.receive(); // None
    • Receives a value from the channel with a timeout.

      Like receive(), but returns None if the operation cannot complete within the specified timeout.

      Parameters

      • ms: number

        Timeout in milliseconds.

      Returns AsyncOption<T>

      A promise that resolves to Some(value) or None if timed out, empty, or closed.

      const ch = Channel<number>(10);
      const v = await ch.receiveTimeout(5);
      console.log(v.isNone()); // true
    • Sends a value into the channel, waiting if necessary.

      • If there are waiting receivers, delivers directly to one of them.
      • If the buffer has space, adds to the buffer and returns immediately.
      • If the buffer is full (or capacity is 0), waits until space is available.
      • If the channel is closed, returns false immediately.

      Parameters

      • value: T

        The value to send.

      Returns Promise<boolean>

      A promise that resolves to true if sent successfully, false if the channel is closed.

      const ch = Channel<number>(1);

      const ok1 = await ch.send(1); // true
      ch.close();
      const ok2 = await ch.send(2); // false
    • Sends a value into the channel with a timeout.

      Like send(), but returns false if the operation cannot complete within the specified timeout.

      Parameters

      • value: T

        The value to send.

      • ms: number

        Timeout in milliseconds.

      Returns Promise<boolean>

      A promise that resolves to true if sent successfully, false if timed out, channel is full, or closed.

      const ch = Channel<number>(0);

      // No receiver arrives within 10ms
      const ok = await ch.sendTimeout(123, 10); // false
    • Custom toString implementation.

      Returns string

      const ch = Channel<number>(10);
      console.log(ch.toString()); // 'Channel(0/10)'

      ch.send(1);
      console.log(ch.toString()); // 'Channel(1/10)'

      ch.close();
      console.log(ch.toString()); // 'Channel(<closed>)'
    • Attempts to receive a value without waiting.

      • If the buffer has values, returns Some(value).
      • If senders are waiting (rendezvous), receives directly from one.
      • Otherwise returns None.

      Returns Option<T>

      Some(value) if available, None if empty.

      const ch = Channel<number>(10);
      console.log(ch.tryReceive().isNone()); // true
    • Attempts to send a value without waiting.

      • If there are waiting receivers, delivers directly and returns true.
      • If the buffer has space, adds to the buffer and returns true.
      • If the buffer is full or the channel is closed, returns false.

      Parameters

      • value: T

        The value to send.

      Returns boolean

      true if sent successfully, false if full or closed.

      const ch = Channel<number>(0);

      // Rendezvous channels are always "full" unless a receiver is waiting
      const ok = ch.trySend(1); // usually false