import { AsyncIterableX } from './asynciterablex' ; import { wrapWithAbort } from './operators/withabort' ; import { throwIfAborted } from '../aborterror' ; import { safeRace } from '../util/safeRace' ; const NEVER_PROMISE = new Promise ( ( ) => { } ) ; type MergeResult < T > = { value : T ; index : number ; } ; function wrapPromiseWithIndex < T > ( promise , index ) { return promise . then ( ( value ) => ( { value , index } ) ) as Promise < MergeResult < T > > ; } export class MergeAsyncIterable < T > extends AsyncIterableX < T > { private _source : AsyncIterable < T > [ ] ; constructor ( source ) { super ( ) ; this . _source = source ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; const length = this . _source . length ; const iterators = new Array < AsyncIterator < T > > ( length ) ; const nexts = new Array < Promise < MergeResult < IteratorResult < T > > > > ( length ) ; let active = length ; for ( let i = 0 ; i < length ; i ++ ) { const iterator = wrapWithAbort ( this . _source [ i ] , signal ) [ Symbol . asyncIterator ] ( ) ; iterators [ i ] = iterator ; nexts [ i ] = wrapPromiseWithIndex ( iterator . next ( ) , i ) ; } while ( active > 0 ) { const next = safeRace ( nexts ) ; const { value : { done : done$ , value : value$ } , index , } = await next ; if ( done$ ) { nexts [ index ] = < Promise < MergeResult < IteratorResult < T > > > > NEVER_PROMISE ; active -- ; } else { const iterator$ = iterators [ index ] ; nexts [ index ] = wrapPromiseWithIndex ( iterator$ . next ( ) , index ) ; yield value$ ; } } } } template T template T2 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } v2 returns { ( AsyncIterableX < T | T2 > ) } export function merge < T , T2 > ( source , v2 ) ; template T template T2 template T3 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } v2 param { AsyncIterable < T3 > } v3 returns { ( AsyncIterableX < T | T2 | T3 > ) } export function merge < T , T2 , T3 > ( source , v2 , v3 ) ; template T template T2 template T3 template T4 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } v2 param { AsyncIterable < T3 > } v3 param { AsyncIterable < T4 > } v4 returns { ( AsyncIterableX < T | T2 | T3 | T4 > ) } export function merge < T , T2 , T3 , T4 > ( source , v2 , v3 , v4 ) ; template T template T2 template T3 template T4 template T5 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } v2 param { AsyncIterable < T3 > } v3 param { AsyncIterable < T4 > } v4 param { AsyncIterable < T5 > } v5 returns { ( AsyncIterable < T | T2 | T3 | T4 | T5 > ) } export function merge < T , T2 , T3 , T4 , T5 > ( source , v2 , v3 , v4 , v5 ) ; template T template T2 template T3 template T4 template T5 template T6 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } v2 param { AsyncIterable < T3 > } v3 param { AsyncIterable < T4 > } v4 param { AsyncIterable < T5 > } v5 param { AsyncIterable < T6 > } v6 returns { ( AsyncIterable < T | T2 | T3 | T4 | T5 | T6 > ) } export function merge < T , T2 , T3 , T4 , T5 , T6 > ( source , v2 , v3 , v4 , v5 , v6 ) ; template T param { AsyncIterable < T > } source param { ... AsyncIterable < T > [ ] } args returns { AsyncIterableX < T > } export function merge < T > ( source , ... args ) ; export function merge < T > ( source , ... args ) { return new MergeAsyncIterable < T > ( [ source , ... args ] ) ; }