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AAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;SACrD;QAED,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC7B,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC;QAE3C,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YACf,MAAM,KAAK,CAAC,KAAK,CAAC;SACnB;QAED,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAEnC,gCAAgC;QAChC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;YAC/B,IAAI,CAAC,KAAK,KAAK,EAAE;gBACf,SAAS;aACV;YAED,MAAM,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;YACnC,IAAI,aAAa,CAAC,MAAM,EAAE;gBACxB,aAAa,CAAC,MAAM,EAAE,CAAC;aACxB;SACF;QAED,IAAI,QAAQ,CAAC;QACb,OAAO,CAAC,CAAC,QAAQ,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE;YAChD,MAAM,QAAQ,CAAC,KAAK,CAAC;SACtB;IACH,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAU,GAAG,OAAiC;IAChE,OAAO,IAAI,iBAAiB,CAAU,OAAO,CAAC,CAAC;AACjD,CAAC","file":"race.js","sourcesContent":["import { AsyncIterableX } from './asynciterablex';\nimport { wrapWithAbort } from './operators/withabort';\nimport { throwIfAborted } from '../aborterror';\nimport { safeRace } from '../util/safeRace';\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\nexport class RaceAsyncIterable<TSource> extends AsyncIterableX<TSource> {\n private _sources: AsyncIterable<TSource>[];\n\n constructor(sources: AsyncIterable<TSource>[]) {\n super();\n this._sources = sources;\n }\n\n async *[Symbol.asyncIterator](signal?: AbortSignal) {\n throwIfAborted(signal);\n const sources = this._sources;\n const length = sources.length;\n\n const iterators = new Array<AsyncIterator<TSource>>(length);\n const nexts = new Array<Promise<MergeResult<IteratorResult<TSource>>>>(length);\n\n for (let i = 0; i < length; i++) {\n const iterator = wrapWithAbort<TSource>(sources[i], signal)[Symbol.asyncIterator]();\n iterators[i] = iterator;\n nexts[i] = wrapPromiseWithIndex(iterator.next(), i);\n }\n\n const next = safeRace(nexts);\n const { value: next$, index } = await next;\n\n if (!next$.done) {\n yield next$.value;\n }\n\n const iterator$ = iterators[index];\n\n // Cancel/finish other iterators\n for (let i = 0; i < length; i++) {\n if (i === index) {\n continue;\n }\n\n const otherIterator = iterators[i];\n if (otherIterator.return) {\n otherIterator.return();\n }\n }\n\n let nextItem;\n while (!(nextItem = await iterator$.next()).done) {\n yield nextItem.value;\n }\n }\n}\n\n/**\n * Propagates the async sequence that reacts first.\n *\n * @export\n * @param {...AsyncIterable<T>[]} sources The source sequences.\n * @return {AsyncIterable<T>} An async sequence that surfaces either of the given sequences, whichever reacted first.\n */\nexport function race<TSource>(...sources: AsyncIterable<TSource>[]): AsyncIterableX<TSource> {\n return new RaceAsyncIterable<TSource>(sources);\n}\n"]}
\No newline at end of file