import { AsyncIterableX } from '../asynciterablex' ; import { arrayIndexOfAsync } from '../../util/arrayindexof' ; import { comparerAsync } from '../../util/comparer' ; import { MonoTypeOperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; export class UnionAsyncIterable < TSource > extends AsyncIterableX < TSource > { private _left : AsyncIterable < TSource > ; private _right : AsyncIterable < TSource > ; private _comparer : ( x : TSource , y : TSource ) => boolean | Promise < boolean > ; constructor ( left , right , comparer ) { super ( ) ; this . _left = left ; this . _right = right ; this . _comparer = comparer ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; const map = [ ] as TSource [ ] ; for await ( const lItem of wrapWithAbort ( this . _left , signal ) ) { if ( ( await arrayIndexOfAsync ( map , lItem , this . _comparer ) ) === - 1 ) { map . push ( lItem ) ; yield lItem ; } } for await ( const rItem of wrapWithAbort ( this . _right , signal ) ) { if ( ( await arrayIndexOfAsync ( map , rItem , this . _comparer ) ) === - 1 ) { map . push ( rItem ) ; yield rItem ; } } } } template TSource param { AsyncIterable < TSource > } right param { ( ( x : TSource , y : TSource ) => boolean | Promise < boolean > ) } comparer returns { MonoTypeOperatorAsyncFunction < TSource > } export function union < TSource > ( right , comparer = comparerAsync ) { return function unionOperatorFunction ( left ) : AsyncIterableX < TSource > { return new UnionAsyncIterable < TSource > ( left , right , comparer ) ; } ; }