import { AsyncIterableX } from '../asynciterablex.js'; import { OperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class SkipWhileAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; private _predicate: ( value: TSource, index: number, signal?: AbortSignal ) => boolean | Promise; constructor( source: AsyncIterable, predicate: (value: TSource, index: number, signal?: AbortSignal) => boolean | Promise ) { super(); this._source = source; this._predicate = predicate; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); let yielding = false; let i = 0; for await (const element of wrapWithAbort(this._source, signal)) { if (!yielding && !(await this._predicate(element, i++, signal))) { yielding = true; } if (yielding) { yield element; } } } } /** * Bypasses elements in an async-iterale sequence as long as a specified condition is true * and then returns the remaining elements. * * @template T The type of the elements in the source sequence. * @template S The result of the predicate that is truthy/falsy. * @param {(value: T, index: number, signal?: AbortSignal) => value is S} predicate A function to test each element for a condition. * @returns {OperatorAsyncFunction} An async-iterable sequence that contains the elements from the input * sequence starting at the first element in the linear series that does not pass the test specified by predicate. */ export function skipWhile( predicate: (value: T, index: number, signal?: AbortSignal) => value is S ): OperatorAsyncFunction; /** * Bypasses elements in an async-iterale sequence as long as a specified condition is true * and then returns the remaining elements. * * @template T The type of the elements in the source sequence. * @param {((value: T, index: number, signal?: AbortSignal) => boolean | Promise)} predicate A function to test each element for a condition. * @returns {OperatorAsyncFunction} An async-iterable sequence that contains the elements from the input * sequence starting at the first element in the linear series that does not pass the test specified by predicate. */ export function skipWhile( predicate: (value: T, index: number, signal?: AbortSignal) => boolean | Promise ): OperatorAsyncFunction; /** * Bypasses elements in an async-iterale sequence as long as a specified condition is true * and then returns the remaining elements. * * @template T The type of the elements in the source sequence. * @param {((value: T, index: number, signal?: AbortSignal) => boolean | Promise)} predicate A function to test each element for a condition. * @returns {OperatorAsyncFunction} An async-iterable sequence that contains the elements from the input * sequence starting at the first element in the linear series that does not pass the test specified by predicate. */ export function skipWhile( predicate: (value: T, index: number) => boolean | Promise ): OperatorAsyncFunction { return function skipWhileOperatorFunction(source: AsyncIterable): AsyncIterableX { return new SkipWhileAsyncIterable(source, predicate); }; }