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 DelayEachAsyncIterable 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); for await (const item of wrapWithAbort(this._source, signal)) { await sleep(this._dueTime, signal); yield item; } } } /** * Delays the emitting of each items 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 takes an async-iterable and delays each item in the sequence by the given time. */ export function delayEach(dueTime: number): MonoTypeOperatorAsyncFunction { return function delayEachOperatorFunction( source: AsyncIterable ): AsyncIterableX { return new DelayEachAsyncIterable(source, dueTime); }; }