happy-rusty
    Preparing search index...

    Interface Sender<T>

    A sender view of a channel that can only send values.

    Created by calling channel.sender(). Shares state with the parent channel.

    interface Sender<T> {
        "[toStringTag]": "Sender";
        capacity: number;
        isClosed: boolean;
        isEmpty: boolean;
        isFull: boolean;
        length: number;
        send(value: T): Promise<boolean>;
        sendTimeout(value: T, ms: number): Promise<boolean>;
        toString(): string;
        trySend(value: T): boolean;
    }

    Type Parameters

    • T

      The type of values that can be sent.

    Index

    Properties

    "[toStringTag]": "Sender"

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

    capacity: number

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

    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.

    isFull: boolean

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

    length: number

    The current number of values in the buffer.

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

    Methods

    • 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 success = await sender.send(42);
      if (!success) {
      console.log('Channel was closed');
      }
    • 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 success = await sender.sendTimeout(42, 1000);
      if (!success) {
      console.log('Send timed out or channel closed');
      }
    • Custom toString implementation.

      Returns string

      const sender = Channel<number>(10).sender;
      console.log(sender.toString()); // 'Sender(0/10)'
    • 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.

      if (!sender.trySend(42)) {
      console.log('Channel is full or closed');
      }