import { AsyncIterableX } from '../asynciterablex.js'; import { MonoTypeOperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class ReverseAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; constructor(source: AsyncIterable) { super(); this._source = source; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); const results = [] as TSource[]; for await (const item of wrapWithAbort(this._source, signal)) { results.unshift(item); } yield* results; } } /** * Reverses the async-iterable instance. * * @template TSource The type of the elements in the source sequence. * @returns {MonoTypeOperatorAsyncFunction} The async-iterable in reversed sequence. */ export function reverse(): MonoTypeOperatorAsyncFunction { return function reverseOperatorFunction(source: AsyncIterable): AsyncIterableX { return new ReverseAsyncIterable(source); }; }