import type { PipeSource, PipeTarget } from '../protocol/pipeable'; import { read } from '../protocol/pipeable'; /** * Indicates that the buffered items in queue has reached its capacity * * @internal */ declare class ClosedChannelError extends Error { message: string; } /** * Indicates that the buffered items in queue has reached its capacity * * @internal */ declare class ChannelFullError extends Error { message: string; } /** * Indicates that the buffered items in queue has reached its capacity * * @internal */ export interface ChannelStream extends AsyncIterable { next: () => Promise; } /** * @internal */ export interface ChannelPipeOptions { /** * Called when `target[read]` throws e.g. pipe a closed target channel. * * param will be called immediately every time the read throws an error. */ onPipeError?: (err: unknown) => any; } /** * * Promise based multi producer single consumer channel * * * - buffered message queue * * - `send` / `receive` basic message passing * * - `pipe` piping to other channels (or use `pipe.to()`) * * - `stream` ES6 async iterator api * * - `freeze` temporarily block all consumers, useful if your target has limited rate of consumption like Node.js net.Socket * * @typeParam T - type of messages in queue * @public */ export declare class Channel implements PipeSource, PipeTarget { /** * ``` * class ClosedChannelError extends Error * ``` */ static ClosedChannelError: typeof ClosedChannelError; /** * ``` * class ChannelFullError extends Error * ``` */ static ChannelFullError: typeof ChannelFullError; private _closed; private _capacity; private queue; private sendSem; private recvSem; private pipeTarget; private pipeOptions?; private paused?; /** * should use sendSem to control maximum messages queued, * `false` if capacity is Infinity */ private bounded; /** * create a new channel with specified capacity * @typeParam T - type of messages in queue * @param capacity - channel capacity, defaults to `Infinity` * * @throws RangeError - capacity is negative or NaN */ constructor(capacity?: number); /** * send a value to channel. * * if the channel has reached its capacity, then call to `send` will be blocked * * @throws -{@link Channel.ClosedChannelError} throw if channel is closed */ send(value: T): Promise; /** * retrieve a value from channel. * * will never resolve if {@link Channel.pipe} or is enabled; * will race with {@link Channel.stream} */ receive(): Promise; /** * try to send a value synchronosly * * @throws -{@link Channel.ClosedChannelError} channel is closed * @throws -{@link Channel.ChannelFullError} channel is full */ trySend(value: T): void; /** * send a promise to channel, after the promise is resolved, send its fullfilled value */ sendAsync(value: Promise): Promise; /** * try receive one message * @returns message `T` or `undefined` if no messages in the queue */ tryReceive(): T | undefined; /** * get a stream to read from the channel, internally uses {@link Channel.receive} */ stream(): ChannelStream; /** * @internal */ [read](value: T): void; /** * pipe channel output to target * * there is only one target at the same time, use `ChannelHub` if you want to have multiple readers */ pipe(target: PipeTarget, options?: ChannelPipeOptions): void; /** * unlink output with target, future input will be stored in channel's internal buffer */ unpipe(): void; /** * stop {@link Channel.stream} / {@link Channel.pipe} / {@link Channel.receive} new items until {@link Channel.resume} is called * * items sending to the channel will be queued despite pipe enabled */ pause(): void; /** * resume the channel so {@link Channel.stream} / {@link Channel.pipe} / {@link Channel.receive} can continue to handle new messages */ resume(): void; /** * close the channel, future `send` will throw a {@link Channel.ClosedChannelError} */ close(): void; /** * check if channel has been closed */ get closed(): boolean; /** * Get the number of maximum items in queue */ get capacity(): number; /** * Get the number of current queued items */ get size(): number; /** * **SHOULD** check `capacity` and `closed` state before calling this method. * * if check inside `writeValue`, there is a chance that `close` is called immediately after `send` * while writeValue is `asynchronosly` called in `send` and will unexpectedly throw an error */ private writeValue; /** * empty current queue by writing all values to pipe target */ private flushQueue; /** * write single item to pipe target */ private flushValue; private next; } export {}; //# sourceMappingURL=channel.d.ts.map