import { AsyncIterableX } from '../asynciterablex.js'; import { arrayIndexOfAsync } from '../../util/arrayindexof.js'; import { comparerAsync } from '../../util/comparer.js'; import { MonoTypeOperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; async function arrayRemove( array: T[], item: T, comparer: (x: T, y: T) => boolean | Promise, signal?: AbortSignal ): Promise { throwIfAborted(signal); const idx = await arrayIndexOfAsync(array, item, comparer); if (idx === -1) { return false; } array.splice(idx, 1); return true; } /** @ignore */ export class IntersectAsyncIterable extends AsyncIterableX { private _first: AsyncIterable; private _second: AsyncIterable; private _comparer: (x: TSource, y: TSource) => boolean | Promise; constructor( first: AsyncIterable, second: AsyncIterable, comparer: (x: TSource, y: TSource) => boolean | Promise ) { super(); this._first = first; this._second = second; this._comparer = comparer; } async *[Symbol.asyncIterator](signal?: AbortSignal) { const map = [] as TSource[]; for await (const secondItem of wrapWithAbort(this._second, signal)) { map.push(secondItem); } for await (const firstItem of wrapWithAbort(this._first, signal)) { if (await arrayRemove(map, firstItem, this._comparer, signal)) { yield firstItem; } } } } /** * Produces the set intersection of two async-iterable sequences. * * @template TSource The type of the elements of the input sequences. * @param {AsyncIterable} second An async-iterable sequence whose distinct elements that also * appear in the first sequence will be returned. * @param {((x: TSource, y: TSource) => boolean | Promise)} [comparer=comparerAsync] An equality comparer to compare values. * @returns {MonoTypeOperatorAsyncFunction} An operator that returns a sequence that contains the elements that form the set * intersection of two sequences. */ export function intersect( second: AsyncIterable, comparer: (x: TSource, y: TSource) => boolean | Promise = comparerAsync ): MonoTypeOperatorAsyncFunction { return function intersectOperatorFunction( first: AsyncIterable ): AsyncIterableX { return new IntersectAsyncIterable(first, second, comparer); }; }