import { AsyncIterableX } from '../asynciterablex.js'; import { MonoTypeOperatorAsyncFunction } from '../../interfaces.js'; import { sleep } from '../_sleep.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class DelayAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; private _dueTime: number; constructor(source: AsyncIterable, dueTime: number) { super(); this._source = source; this._dueTime = dueTime; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); await sleep(this._dueTime, signal); for await (const item of wrapWithAbort(this._source, signal)) { yield item; } } } /** * Delays the emitting of the first item in the async-iterable by the given due time. * * @template TSource The type of elements in the source sequence. * @param {number} dueTime The delay duration in milliseconds * @returns {MonoTypeOperatorAsyncFunction} An operator which delays the before the iteration begins. */ export function delay(dueTime: number): MonoTypeOperatorAsyncFunction { return function delayOperatorFunction(source: AsyncIterable): AsyncIterableX { return new DelayAsyncIterable(source, dueTime); }; }