import { Context } from '@dxos/context'; import { type MaybePromise } from '@dxos/util'; type Callbacks = { ctx: Context; /** * Advises that the producer is ready to stream the data. * Called automatically with the first call to `next`. */ ready: () => void; /** * Sends a message into the stream. */ next: (message: T) => void; /** * Closes the stream. * Optional error can be provided. */ close: (err?: Error) => void; }; type Producer = (callbacks: Callbacks) => ((err?: Error) => void) | void; export type StreamItem = { ready: true; } | { data: T; } | { closed: true; error?: Error; }; /** * Represents a typed stream of data. * In concept it's a Promise that can resolve multiple times. * Can only have one subscriber. * `close` must be called to clean-up the resources. */ export declare class Stream { /** * Consumes the entire stream to the end until it closes and returns a promise with the resulting items. */ static consume(stream: Stream): Promise[]>; static consumeData(stream: Stream): Promise; static first(stream: Stream): Promise; /** * Maps all data coming through the stream. */ static map(source: Stream, map: (data: T) => U): Stream; /** * Converts Promise> to Stream. */ static unwrapPromise(streamPromise: MaybePromise>): Stream; static fromPromise(promise: Promise): Stream; private readonly _ctx; private _messageHandler?; private _closeHandler?; private _readyHandler?; private _isClosed; private _closeError; private _producerCleanup; private _readyPromise; private _resolveReadyPromise; private _isReady; /** * Buffer messages before subscription. Set to null when buffer is no longer needed. */ private _buffer; constructor(producer: Producer, parentCtx?: Context); private _markAsReady; subscribe(onMessage: (msg: T) => void, onClose?: (err?: Error) => void): void; /** * Resolves when stream is ready. */ waitUntilReady(): Promise; /** * Registers a callback to be called when stream is ready. */ onReady(onReady: () => void): void; /** * Close the stream and dispose of any resources. */ close(): Promise; } /** * Resolves with the first value the stream emits, then closes it. * Rejects if the stream closes first — with the stream's error where it had one — so a caller * without a `timeout` cannot wait forever on a stream that will never emit. */ export declare const getFirstStreamValue: (stream: Stream, { timeout }?: { timeout?: number; }) => Promise; export {}; //# sourceMappingURL=stream.d.ts.map