happy-rusty
    Preparing search index...

    Interface Receiver<T>

    A receiver view of a channel that can only receive values.

    Created by calling channel.receiver(). Shares state with the parent channel. Implements AsyncIterable for use with for await...of.

    interface Receiver<T> {
        "[toStringTag]": "Receiver";
        capacity: number;
        isClosed: boolean;
        isEmpty: boolean;
        isFull: boolean;
        length: number;
        "[asyncIterator]"(): AsyncIterator<T>;
        receive(): AsyncOption<T>;
        receiveTimeout(ms: number): AsyncOption<T>;
        toString(): string;
        tryReceive(): Option<T>;
    }

    Type Parameters

    • T

      The type of values that can be received.

    Index

    Properties

    "[toStringTag]": "Receiver"

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

    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

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

      Returns AsyncIterator<T>

      for await (const msg of receiver) {
      console.log('Message:', msg);
      }
      console.log('Channel closed');
    • 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 result = await receiver.receive();
      if (result.isSome()) {
      console.log('Received:', result.unwrap());
      } else {
      console.log('Channel closed');
      }
    • 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 result = await receiver.receiveTimeout(1000);
      if (result.isNone()) {
      console.log('Receive timed out or channel closed');
      }
    • Custom toString implementation.

      Returns string

      const receiver = Channel<number>(10).receiver;
      console.log(receiver.toString()); // 'Receiver(0/10)'
    • 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 result = receiver.tryReceive();
      result.inspect((v) => console.log('Got:', v));