/** * PushQueue — Single-producer, single-consumer async iterator. * * The core async primitive used to bridge the gap between the background * message routing loop and the consumer's for-await-of iteration. * * Design: One pending Promise slot. If consumer awaits next() before * producer enqueues, the item resolves immediately (zero-copy path). * Otherwise items accumulate in the buffer array. * * @example * ```ts * const queue = new PushQueue(); * * // Producer side * queue.enqueue(1); * queue.enqueue(2); * queue.enqueue(3); * queue.done(); * * // Consumer side * for await (const item of queue) { * console.log(item); // 1, 2, 3 * } * ``` */ export declare class PushQueue implements AsyncIterableIterator { /** * Internal buffer for items that have been enqueued but not yet consumed. */ private queue; /** * Resolve function for the pending next() call, if the consumer is waiting. */ private readResolve?; /** * Reject function for the pending next() call, if the consumer is waiting. */ private readReject?; /** * Whether the producer has signaled completion via done(). */ private isDone; /** * If set, the error that will be thrown on the next (or pending) next() call. */ private errorValue?; /** * Whether iteration has begun. Enforces single-consumer semantics. */ private started; /** * Optional callback invoked when the consumer breaks out of for-await-of * (which triggers the iterator's return() method). */ private returnCallback?; /** * @param onReturn - Optional callback invoked when the consumer stops * iterating (e.g., via `break` in a for-await-of loop). Useful for * triggering cleanup of upstream resources. */ constructor(onReturn?: () => void); /** * Returns this queue as an async iterable iterator. * Can only be called once — the queue is single-consumer. * * @throws Error if called more than once */ [Symbol.asyncIterator](): AsyncIterableIterator; /** * Returns the next item in the queue. * * - If items are buffered, returns the first one immediately. * - If the stream is done, returns `{ done: true }`. * - If an error has been set, throws it. * - Otherwise, returns a Promise that resolves when the next item * is enqueued (or done/error is signaled). */ next(): Promise>; /** * Enqueue an item for the consumer. * * If a consumer is currently awaiting next(), the item is delivered * immediately without buffering (zero-copy fast path). Otherwise the * item is pushed to the internal buffer. * * Items enqueued after done() or error() are silently dropped. */ enqueue(item: T): void; /** * Signal that no more items will be enqueued. * * If the consumer is waiting on next(), it receives `{ done: true }`. * Any buffered items are still delivered before the done signal. */ done(): void; /** * Signal an error. The next (or currently pending) next() call will * throw the provided error. * * @param err - The error to propagate to the consumer. */ error(err: unknown): void; /** * Called when the consumer breaks out of a for-await-of loop. * Marks the stream as done and invokes the optional return callback. */ return(): Promise>; /** * Called when the consumer throws an error back into the iterator. * Propagates the error and signals completion. */ throw(e?: unknown): Promise>; }