UNPKG

1.24 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { throwIfAborted } from '../aborterror';
3export class RepeatValueAsyncIterable extends AsyncIterableX {
4 constructor(value, count) {
5 super();
6 this._value = value;
7 this._count = count;
8 }
9 async *[Symbol.asyncIterator](signal) {
10 if (this._count === -1) {
11 while (1) {
12 throwIfAborted(signal);
13 yield this._value;
14 }
15 }
16 else {
17 for (let i = 0; i < this._count; i++) {
18 throwIfAborted(signal);
19 yield this._value;
20 }
21 }
22 }
23}
24/**
25 * Repeats a given value for the specified number of times as an async-iterable.
26 *
27 * @export
28 * @template TSource The type of element to repeat.
29 * @param {TSource} value The value to repeat as an async-iterable.
30 * @param {number} [count=-1] The number of times to repeat the value, infinite if not specified.
31 * @returns {AsyncIterableX<TSource>} An async-iterable with a single item that is repeated over the specified times.
32 */
33export function repeatValue(value, count = -1) {
34 return new RepeatValueAsyncIterable(value, count);
35}
36
37//# sourceMappingURL=repeatvalue.mjs.map