import { IterableX } from '../iterablex.js'; import { MonoTypeOperatorFunction } from '../../interfaces.js'; /** @ignore */ export class RepeatIterable extends IterableX { private _source: Iterable; private _count: number; constructor(source: Iterable, count: number) { super(); this._source = source; this._count = count; } *[Symbol.iterator]() { if (this._count === -1) { while (1) { for (const item of this._source) { yield item; } } } else { for (let i = 0; i < this._count; i++) { for (const item of this._source) { 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 {MonoTypeOperatorFunction} The iterable sequence producing the elements of the given sequence repeatedly. */ export function repeat(count = -1): MonoTypeOperatorFunction { return function repeatOperatorFunction(source: Iterable): IterableX { return new RepeatIterable(source, count); }; }