import { AsyncIterableX } from '../asynciterablex.js'; import { MonoTypeOperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class RepeatAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; private _count: number; constructor(source: AsyncIterable, count: number) { super(); this._source = source; this._count = count; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); if (this._count === -1) { while (1) { for await (const item of wrapWithAbort(this._source, signal)) { yield item; } } } else { for (let i = 0; i < this._count; i++) { for await (const item of wrapWithAbort(this._source, signal)) { yield item; } } } } } /** * Repeats the async-enumerable sequence a specified number of times. * * @template TSource The type of the elements in the source sequence. * @param {number} [count=-1] Number of times to repeat the sequence. If not specified, the sequence repeats indefinitely. * @returns {MonoTypeOperatorAsyncFunction} The async-iterable sequence producing the elements of the given sequence repeatedly. */ export function repeat(count = -1): MonoTypeOperatorAsyncFunction { return function repeatOperatorFunction(source: AsyncIterable): AsyncIterableX { return new RepeatAsyncIterable(source, count); }; }