import { AsyncIterableX } from '../asynciterablex.js'; import { MonoTypeOperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class SkipLastAsyncIterable extends AsyncIterableX { private _source: AsyncIterable; private _count: number; constructor(source: AsyncIterable, count: number) { super(); this._source = source; this._count = count; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); const q = [] as TSource[]; for await (const item of wrapWithAbort(this._source, signal)) { q.push(item); if (q.length > this._count) { yield q.shift()!; } } } } /** * Bypasses a specified number of elements at the end of an async-iterable sequence. * * @template TSource The type of the elements in the source sequence. * @param {number} count Number of elements to bypass at the end of the source sequence. * @returns {MonoTypeOperatorAsyncFunction} An async-iterable sequence containing the * source sequence elements except for the bypassed ones at the end. */ export function skipLast(count: number): MonoTypeOperatorAsyncFunction { return function skipLastOperatorFunction( source: AsyncIterable ): AsyncIterableX { return new SkipLastAsyncIterable(source, count); }; }