import { AsyncIterableX } from '../asynciterablex.js'; import { createGrouping } from './_grouping.js'; import { empty } from '../empty.js'; import { identity } from '../../util/identity.js'; import { OperatorAsyncFunction } from '../../interfaces.js'; import { wrapWithAbort } from './withabort.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ export class GroupJoinAsyncIterable extends AsyncIterableX { private _outer: AsyncIterable; private _inner: AsyncIterable; private _outerSelector: (value: TOuter, signal?: AbortSignal) => TKey | Promise; private _innerSelector: (value: TInner, signal?: AbortSignal) => TKey | Promise; private _resultSelector: ( outer: TOuter, inner: AsyncIterable, signal?: AbortSignal ) => TResult | Promise; constructor( outer: AsyncIterable, inner: AsyncIterable, outerSelector: (value: TOuter, signal?: AbortSignal) => TKey | Promise, innerSelector: (value: TInner, signal?: AbortSignal) => TKey | Promise, resultSelector: ( outer: TOuter, inner: AsyncIterable, signal?: AbortSignal ) => TResult | Promise ) { super(); this._outer = outer; this._inner = inner; this._outerSelector = outerSelector; this._innerSelector = innerSelector; this._resultSelector = resultSelector; } async *[Symbol.asyncIterator](signal?: AbortSignal) { throwIfAborted(signal); const map = await createGrouping(this._inner, this._innerSelector, identity, signal); for await (const outerElement of wrapWithAbort(this._outer, signal)) { const outerKey = await this._outerSelector(outerElement, signal); const innerElements = map.has(outerKey) ? >map.get(outerKey) : empty(); yield await this._resultSelector(outerElement, AsyncIterableX.as(innerElements), signal); } } } /** * Correlates the elements of two async-iterable sequences based on equality of keys and groups the results. * * @template TOuter The type of the elements of the first async-iterable sequence. * @template TInner The type of the elements of the second async-iterable sequence. * @template TKey The type of the keys returned by the key selector functions. * @template TResult The type of the result elements. * @param {AsyncIterable} inner The async-enumerable sequence to join to the first sequence. * @param {((value: TOuter, signal?: AbortSignal) => TKey | Promise)} outerSelector A function to extract the join key from each * element of the first sequence. * @param {((value: TInner, signal?: AbortSignal) => TKey | Promise)} innerSelector A function to extract the join key from each * element of the second sequence. * @param {(( * outer: TOuter, * inner: AsyncIterable, * signal?: AbortSignal * ) => TResult | Promise)} resultSelector A function to create a result element from an element from the first sequence and a * collection of matching elements from the second sequence. * @returns {OperatorAsyncFunction} An operator that returns an async-iterable sequence that contains the result elements * that are obtained by performing a grouped join on two sequences. */ export function groupJoin( inner: AsyncIterable, outerSelector: (value: TOuter, signal?: AbortSignal) => TKey | Promise, innerSelector: (value: TInner, signal?: AbortSignal) => TKey | Promise, resultSelector: ( outer: TOuter, inner: AsyncIterable, signal?: AbortSignal ) => TResult | Promise ): OperatorAsyncFunction { return function groupJoinOperatorFunction(outer: AsyncIterable): AsyncIterableX { return new GroupJoinAsyncIterable( outer, inner, outerSelector, innerSelector, resultSelector ); }; }