Skip to main content

interface Channel<T>

A Rimbu Channel offers various ways to synchronize communication between asynchronous processes. These processes can send and receive messages in a blocking way. Channel messages are of type T, and channels can be buffered or unbuffered. A buffered channel can queue a given amount of messages before blocking the sender.

Companion namespace: Channel

Extends: Channel.Write<T>, Channel.Read<T>

Type parameters

NameDefaultDescription
Tvoidthe channel message type

Properties

capacity

The maximum amount of messages the Channel can buffer. If 0, the channel is unbuffered and the communication is synchronous.

Definition

get capacity(): number;

Overrides

Read.capacity

isClosed

Returns true if the Channel is closed.

Definition

get isClosed(): boolean;

Overrides

Write.isClosed

isExhausted

Returns true if the channel is closed and there are no message in the buffer (length = 0), false otherwise.

Definition

get isExhausted(): boolean;

Overrides

Read.isExhausted

length

The amount of messages currently in the read buffer.

Definition

get length(): number;

Overrides

Read.length

Methods

asyncStream

Returns an asynchronous stream of values.

Definition

asyncStream(): AsyncStream<T>;

Overrides

AsyncStreamable.asyncStream

close

Closes the channel. After a close, further send actions will throw.

Definition

close(): void;

Overrides

Write.close

readable

Returns the Channel as a readonly Channel.Read instance.

Definition

readable(): Channel.Read<T>;

Overrides

Read.readable

receive

Returns the next message sent to the Channel. Blocks if there are no messages.

Definitions

receive<RT>(options: {
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      recover: (channelError: Channel.Error) => RT;
    }): Promise<T | RT>;

receive(options?: {
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      recover?: undefined;
    }): Promise<T>;

Type parameters

NameDescription
RT

Parameters

NameTypeDescription
options{
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      recover: (channelError: Channel.Error) => RT;
    }
(optional) the options to receive a message
- signal: (optional) an abort signal to cancel receiving
- timeoutMs: (optional) amount of milliseconds to wait for received message
- recover: (optional) a function that can be supplied to recover from a channel error

Overrides

Read.receive

send

Send the given value message to the Channel. Blocks if the Channel is already at maximum capacity.

Definitions

send(value: T, options: {
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      catchChannelErrors?: false | undefined;
    }): Promise<void>;

send(value: T, options?: {
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      catchChannelErrors: boolean;
    }): Promise<undefined | Channel.Error>;

Parameters

NameTypeDescription
valueTthe message to send to the channel
options{
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      catchChannelErrors?: false | undefined;
    }
(optional) the message send options
- signal: (optional) an abort signal to cancel sending
- timeoutMs: (optional) amount of milliseconds to wait for being able to send message
- recover: (optional) a function that can be supplied to recover from a channel error

Overrides

Write.send

sendAll

Sequentially send all the values in the given source to the channel. Blocks until all the values are sent.

Definitions

sendAll(source: AsyncStreamSource<T>, options: {
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      catchChannelErrors?: false | undefined;
    }): Promise<void>;

sendAll(source: AsyncStreamSource<T>, options?: {
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      catchChannelErrors: boolean;
    }): Promise<undefined | Channel.Error>;

Parameters

NameTypeDescription
sourceAsyncStreamSource<T>a stream source containing the values to send
options{
      signal?: AbortSignal | undefined;
      timeoutMs?: number | undefined;
      catchChannelErrors?: false | undefined;
    }
the message send options
- signal: (optional) an abort signal to cancel sending
- timeoutMs: (optional) amount of milliseconds to wait for being able to send message, for each separate message in the source
- recover: (optional) a function that can be supplied to recover from a channel error

Overrides

Write.sendAll

writable

Returns the Channel as a write-only Channel.Write instance.

Definition

writable(): Channel.Write<T>;

Overrides

Write.writable