UNPKG

async-streamify

Version:

Stream and serialize nested promises and async iterables over HTTP, workers, etc

62 lines 1.78 kB
/** * A class that implements an async iterable with buffering capabilities. * Allows pushing values into a buffer that can be consumed asynchronously. * * @template T - The type of values stored in the buffer * * @example * ```typescript * const buffer = new BufferedAsyncIterable<number>(); * * // Push values * buffer.push(1); * buffer.push(2); * buffer.close(); // Below will keep awaiting until this is called. * * // Consume values * for await (const value of buffer) { * console.log(value); // 1, 2 * } * ``` */ export default class BufferedAsyncIterable<T = unknown> { private buffer; private resolvers; protected isClosed: boolean; /** * Optional callback that is invoked when a consumer is waiting for values */ onWait?: () => void; /** * Optional callback that is invoked when a consumer calls `next()` */ onNext?: () => void; /** * Pushes a new value into the buffer. * If there are waiting consumers, the first one will be resolved with this value. * * @param value - The value to push into the buffer * @returns void */ push(value: T): void; /** * Marks the iterable as done, resolving all waiting consumers with done=true * * @returns void */ close(): void; /** * Returns a promise that resolves with the next value in the buffer. * If the buffer is empty and not done, the promise will wait for the next push. * * @returns Promise<IteratorResult<T>> */ next(): Promise<IteratorResult<T>>; /** * Implementation of the AsyncIterator interface * * @returns AsyncIterator<T> */ [Symbol.asyncIterator](): AsyncIterator<T>; } //# sourceMappingURL=bufferedAsyncIterable.d.ts.map