import { identity } from '../util/identity' ; import { wrapWithAbort } from './operators/withabort' ; 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 > > ; } template T template T2 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 returns { ( Promise < [ T , T2 ] | undefined > ) } export function forkJoin < T , T2 > ( source , source2 ) ; template T template T2 template T3 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 returns { ( Promise < [ T , T2 , T3 ] | undefined > ) } export function forkJoin < T , T2 , T3 > ( source , source2 , source3 ) ; template T template T2 template T3 template T4 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 param { AsyncIterable < T4 > } source4 returns { ( Promise < [ T , T2 , T3 , T4 ] | undefined > ) } export function forkJoin < T , T2 , T3 , T4 > ( source , source2 , source3 , source4 ) ; template T template T2 template T3 template T4 template T5 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 param { AsyncIterable < T4 > } source4 param { AsyncIterable < T5 > } source5 returns { ( Promise < [ T , T2 , T3 , T4 , T5 ] | undefined > ) } export function forkJoin < T , T2 , T3 , T4 , T5 > ( source , source2 , source3 , source4 , source5 ) ; template T template T2 template T3 template T4 template T5 template T6 param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 param { AsyncIterable < T4 > } source4 param { AsyncIterable < T5 > } source5 param { AsyncIterable < T6 > } source6 returns { ( Promise < [ T , T2 , T3 , T4 , T5 , T6 ] | undefined > ) } export function forkJoin < T , T2 , T3 , T4 , T5 , T6 > ( source , source2 , source3 , source4 , source5 , source6 ) ; template T param { AbortSignal } signal param { AsyncIterable < T > } source returns { ( Promise < [ T ] | undefined > ) } export function forkJoin < T > ( signal , source ) ; template T template T2 param { AbortSignal } signal param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 returns { ( Promise < [ T , T2 ] | undefined > ) } export function forkJoin < T , T2 > ( signal , source , source2 ) ; template T template T2 template T3 param { AbortSignal } signal param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 returns { ( Promise < [ T , T2 , T3 ] | undefined > ) } export function forkJoin < T , T2 , T3 > ( signal , source , source2 , source3 ) ; template T template T2 template T3 template T4 param { AbortSignal } signal param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 param { AsyncIterable < T4 > } source4 returns { ( Promise < [ T , T2 , T3 , T4 ] | undefined > ) } export function forkJoin < T , T2 , T3 , T4 > ( signal , source , source2 , source3 , source4 ) ; template T template T2 template T3 template T4 template T5 param { AbortSignal } signal param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 param { AsyncIterable < T4 > } source4 param { AsyncIterable < T5 > } source5 returns { ( Promise < [ T , T2 , T3 , T4 , T5 ] | undefined > ) } export function forkJoin < T , T2 , T3 , T4 , T5 > ( signal , source , source2 , source3 , source4 , source5 ) ; template T template T2 template T3 template T4 template T5 template T6 param { AbortSignal } signal param { AsyncIterable < T > } source param { AsyncIterable < T2 > } source2 param { AsyncIterable < T3 > } source3 param { AsyncIterable < T4 > } source4 param { AsyncIterable < T5 > } source5 param { AsyncIterable < T6 > } source6 returns { ( Promise < [ T , T2 , T3 , T4 , T5 , T6 ] | undefined > ) } export function forkJoin < T , T2 , T3 , T4 , T5 , T6 > ( signal , source , source2 , source3 , source4 , source5 , source6 ) ; template T param { ... AsyncIterable < T > [ ] } sources returns { ( Promise < T [ ] | undefined > ) } export function forkJoin < T > ( ... sources ) ; template T param { AbortSignal } signal param { ... AsyncIterable < T > [ ] } sources returns { ( Promise < T [ ] | undefined > ) } export function forkJoin < T > ( signal , ... sources ) ; template T param { ... any [ ] } sources returns { ( Promise < T [ ] | undefined > ) } export async function forkJoin < T > ( ... sources ) { let signal = sources . shift ( ) as AbortSignal | undefined ; if ( ! ( signal instanceof AbortSignal ) ) { sources . unshift ( signal ) ; signal = undefined ; } const length = sources . length ; const iterators = new Array < AsyncIterator < T > > ( length ) ; const nexts = new Array < Promise < MergeResult < IteratorResult < T > > > > ( length ) ; let active = length ; const values = new Array < T > ( length ) ; const hasValues = new Array < boolean > ( length ) ; hasValues . fill ( false ) ; for ( let i = 0 ; i < length ; i ++ ) { const iterator = wrapWithAbort < T > ( sources [ i ] , signal ) [ Symbol . asyncIterator ] ( ) ; iterators [ i ] = iterator ; nexts [ i ] = wrapPromiseWithIndex ( iterator . next ( ) , i ) ; } while ( active > 0 ) { const next = safeRace ( nexts ) ; const { value : next$ , index } = await next ; if ( next$ . done ) { nexts [ index ] = < Promise < MergeResult < IteratorResult < T > > > > NEVER_PROMISE ; active -- ; } else { const iterator$ = iterators [ index ] ; nexts [ index ] = wrapPromiseWithIndex ( iterator$ . next ( ) , index ) ; hasValues [ index ] = true ; values [ index ] = next$ . value ; } } if ( hasValues . length > 0 && hasValues . every ( identity ) ) { return values ; } return undefined ; }