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,EAAE;QAC/B,MAAM,QAAQ,GAAG,aAAa,CAAI,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;QAC9E,SAAS,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC;QACxB,KAAK,CAAC,CAAC,CAAC,GAAG,oBAAoB,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;KACrD;IAED,OAAO,MAAM,GAAG,CAAC,EAAE;QACjB,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC7B,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC;QAC3C,IAAI,KAAK,CAAC,IAAI,EAAE;YACd,KAAK,CAAC,KAAK,CAAC,GAA4C,aAAa,CAAC;YACtE,MAAM,EAAE,CAAC;SACV;aAAM;YACL,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;YACnC,KAAK,CAAC,KAAK,CAAC,GAAG,oBAAoB,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;YAC7D,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;YACxB,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC;SAC7B;KACF;IAED,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE;QACrD,OAAO,MAAM,CAAC;KACf;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","file":"forkjoin.js","sourcesContent":["import { identity } from '../util/identity';\nimport { wrapWithAbort } from './operators/withabort';\nimport { safeRace } from '../util/safeRace';\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nconst NEVER_PROMISE = new Promise(() => {});\n\ntype MergeResult<T> = { value: T; index: number };\n\nfunction wrapPromiseWithIndex<T>(promise: Promise<T>, index: number) {\n return promise.then((value) => ({ value, index })) as Promise<MergeResult<T>>;\n}\n\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @returns {(Promise<[T, T2] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2>(\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>\n): Promise<[T, T2] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @returns {(Promise<[T, T2, T3] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3>(\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>\n): Promise<[T, T2, T3] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @template T4 The type of the elements in the fourth source sequence.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @param {AsyncIterable<T4>} source4 Fourth async-iterable source.\n * @returns {(Promise<[T, T2, T3, T4] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3, T4>(\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>,\n source4: AsyncIterable<T4>\n): Promise<[T, T2, T3, T4] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @template T4 The type of the elements in the fourth source sequence.\n * @template T5 The type of the elements in the fifth source sequence.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @param {AsyncIterable<T4>} source4 Fourth async-iterable source.\n * @param {AsyncIterable<T5>} source5 Fifth async-iterable source.\n * @returns {(Promise<[T, T2, T3, T4, T5] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3, T4, T5>(\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>,\n source4: AsyncIterable<T4>,\n source5: AsyncIterable<T5>\n): Promise<[T, T2, T3, T4, T5] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @template T4 The type of the elements in the fourth source sequence.\n * @template T5 The type of the elements in the fifth source sequence.\n * @template T6 The type of the elements in the sixth source sequence.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @param {AsyncIterable<T4>} source4 Fourth async-iterable source.\n * @param {AsyncIterable<T5>} source5 Fifth async-iterable source.\n * @param {AsyncIterable<T6>} source6 Sixth async-iterable source.\n * @returns {(Promise<[T, T2, T3, T4, T5, T6] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3, T4, T5, T6>(\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>,\n source4: AsyncIterable<T4>,\n source5: AsyncIterable<T5>,\n source6: AsyncIterable<T6>\n): Promise<[T, T2, T3, T4, T5, T6] | undefined>;\n\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @returns {(Promise<[T] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T>(\n signal: AbortSignal,\n source: AsyncIterable<T>\n): Promise<[T] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @returns {(Promise<[T, T2] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2>(\n signal: AbortSignal,\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>\n): Promise<[T, T2] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @returns {(Promise<[T, T2, T3] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3>(\n signal: AbortSignal,\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>\n): Promise<[T, T2, T3] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @template T4 The type of the elements in the fourth source sequence.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @param {AsyncIterable<T4>} source4 Fourth async-iterable source.\n * @returns {(Promise<[T, T2, T3, T4] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3, T4>(\n signal: AbortSignal,\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>,\n source4: AsyncIterable<T4>\n): Promise<[T, T2, T3, T4] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @template T4 The type of the elements in the fourth source sequence.\n * @template T5 The type of the elements in the fifth source sequence.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @param {AsyncIterable<T4>} source4 Fourth async-iterable source.\n * @param {AsyncIterable<T5>} source5 Fifth async-iterable source.\n * @returns {(Promise<[T, T2, T3, T4, T5] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3, T4, T5>(\n signal: AbortSignal,\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>,\n source4: AsyncIterable<T4>,\n source5: AsyncIterable<T5>\n): Promise<[T, T2, T3, T4, T5] | undefined>;\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the first source sequence.\n * @template T2 The type of the elements in the second source sequence.\n * @template T3 The type of the elements in the third source sequence.\n * @template T4 The type of the elements in the fourth source sequence.\n * @template T5 The type of the elements in the fifth source sequence.\n * @template T6 The type of the elements in the sixth source sequence.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {AsyncIterable<T>} source First async-iterable source.\n * @param {AsyncIterable<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @param {AsyncIterable<T4>} source4 Fourth async-iterable source.\n * @param {AsyncIterable<T5>} source5 Fifth async-iterable source.\n * @param {AsyncIterable<T6>} source6 Sixth async-iterable source.\n * @returns {(Promise<[T, T2, T3, T4, T5, T6] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T, T2, T3, T4, T5, T6>(\n signal: AbortSignal,\n source: AsyncIterable<T>,\n source2: AsyncIterable<T2>,\n source3: AsyncIterable<T3>,\n source4: AsyncIterable<T4>,\n source5: AsyncIterable<T5>,\n source6: AsyncIterable<T6>\n): Promise<[T, T2, T3, T4, T5, T6] | undefined>;\n\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the source sequences.\n * @param {...AsyncIterable<T>[]} sources The source sequences.\n * @returns {(Promise<T[] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T>(...sources: AsyncIterable<T>[]): Promise<T[] | undefined>;\n\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the source sequences.\n * @param {AbortSignal} signal An abort signal used for cancellation at any time.\n * @param {...AsyncIterable<T>[]} sources The source sequences.\n * @returns {(Promise<T[] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport function forkJoin<T>(\n signal: AbortSignal,\n ...sources: AsyncIterable<T>[]\n): Promise<T[] | undefined>;\n\n/**\n * Runs all specified async-iterable sequences in parallel and collects their last elements.\n *\n * @export\n * @template T The type of the elements in the source sequences.\n * @param {...any[]} sources Async-iterable sequence to collect the last elements for.\n * @returns {(Promise<T[] | undefined>)} An async-iterable sequence with an array of all the last elements of all sequences.\n */\nexport async function forkJoin<T>(...sources: any[]): Promise<T[] | undefined> {\n let signal = sources.shift() as AbortSignal | undefined;\n if (!(signal instanceof AbortSignal)) {\n sources.unshift(signal);\n signal = undefined;\n }\n\n const length = sources.length;\n const iterators = new Array<AsyncIterator<T>>(length);\n const nexts = new Array<Promise<MergeResult<IteratorResult<T>>>>(length);\n\n let active = length;\n const values = new Array<T>(length);\n const hasValues = new Array<boolean>(length);\n hasValues.fill(false);\n\n for (let i = 0; i < length; i++) {\n const iterator = wrapWithAbort<T>(sources[i], signal)[Symbol.asyncIterator]();\n iterators[i] = iterator;\n nexts[i] = wrapPromiseWithIndex(iterator.next(), i);\n }\n\n while (active > 0) {\n const next = safeRace(nexts);\n const { value: next$, index } = await next;\n if (next$.done) {\n nexts[index] = <Promise<MergeResult<IteratorResult<T>>>>NEVER_PROMISE;\n active--;\n } else {\n const iterator$ = iterators[index];\n nexts[index] = wrapPromiseWithIndex(iterator$.next(), index);\n hasValues[index] = true;\n values[index] = next$.value;\n }\n }\n\n if (hasValues.length > 0 && hasValues.every(identity)) {\n return values;\n }\n\n return undefined;\n}\n"]}
\No newline at end of file