import { AsyncIterableX } from '../asynciterablex.js'; import { OperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export interface TimeInterval { value: T; elapsed: number; } /** @ignore */ export class TimeIntervalAsyncIterable extends AsyncIterableX> { private _source: AsyncIterable; constructor(source: AsyncIterable) { super(); this._source = source; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); let last = Date.now(); for await (const item of wrapWithAbort(this._source, signal)) { const now = Date.now(); const span = now - last; last = now; yield { value: item, elapsed: span }; } } } /** * Records the time interval between consecutive elements in an async-iterable sequence. * * @template TSource The type of the elements in the source sequence. * @returns {OperatorAsyncFunction>} An async-iterable sequence with time * interval information on elements. */ export function timeInterval(): OperatorAsyncFunction> { return function timeIntervalOperatorFunction( source: AsyncIterable ): AsyncIterableX> { return new TimeIntervalAsyncIterable(source); }; }