UNPKG

1.29 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { wrapWithAbort } from './operators/withabort';
3import { throwIfAborted } from '../aborterror';
4class WhileAsyncIterable extends AsyncIterableX {
5 constructor(condition, source) {
6 super();
7 this._condition = condition;
8 this._source = source;
9 }
10 async *[Symbol.asyncIterator](signal) {
11 throwIfAborted(signal);
12 while (await this._condition(signal)) {
13 for await (const item of wrapWithAbort(this._source, signal)) {
14 yield item;
15 }
16 }
17 }
18}
19/**
20 * Repeats the given source as long as the specified conditions holds, where
21 * the condition is evaluated before each repeated source is iterated.
22 *
23 * @export
24 * @template TSource
25 * @param {AsyncIterable<TSource>} source Source to repeat as long as the condition function evaluates to true.
26 * @param {((signal?: AbortSignal) => boolean | Promise<boolean>)} condition Condition that will be evaluated before the source sequence is iterated.
27 * @returns {AsyncIterableX<TSource>} An async-iterable which is repeated while the condition returns true.
28 */
29export function whileDo(source, condition) {
30 return new WhileAsyncIterable(condition, source);
31}
32
33//# sourceMappingURL=whiledo.mjs.map