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AAK,CAAC,KAAK,CAAC,GAAkD,aAAa,CAAC;aAC7E;iBAAM;gBACL,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC;gBACvB,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;gBACvB,WAAW,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;gBAEvC,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;gBACnC,KAAK,CAAC,KAAK,CAAC,GAAG,oBAAoB,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;aAC9D;SACF;IACH,CAAC;CACF;AAmHD,MAAM,UAAU,cAAc,CAAI,GAAG,OAAc;IACjD,OAAO,SAAS,8BAA8B,CAAC,MAAwB;QACrE,OAAO,IAAI,2BAA2B,CAAI,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7D,CAAC,CAAC;AACJ,CAAC","file":"withlatestfrom.js","sourcesContent":["import { AsyncIterableX } from '../asynciterablex';\nimport { OperatorAsyncFunction } from '../../interfaces';\nimport { wrapWithAbort } from './withabort';\nimport { throwIfAborted } from '../../aborterror';\nimport { identity } from '../../util/identity';\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\nexport class WithLatestFromAsyncIterable<TSource> extends AsyncIterableX<TSource[]> {\n  private _source: AsyncIterable<TSource>;\n  private _others: AsyncIterable<TSource>[];\n\n  constructor(source: AsyncIterable<TSource>, others: AsyncIterable<TSource>[]) {\n    super();\n    this._source = source;\n    this._others = others;\n  }\n\n  async *[Symbol.asyncIterator](signal?: AbortSignal) {\n    throwIfAborted(signal);\n\n    const length = this._others.length;\n    const newLength = length + 1;\n    const iterators = new Array<AsyncIterator<TSource>>(newLength);\n    const nexts = new Array<Promise<MergeResult<IteratorResult<TSource>>>>(newLength);\n\n    let hasValueAll = false;\n    const hasValue = new Array(length);\n    const values = new Array(length);\n    hasValue.fill(false);\n\n    for (let i = 0; i < length; i++) {\n      const iterator = wrapWithAbort(this._others[i], signal)[Symbol.asyncIterator]();\n      iterators[i] = iterator;\n      nexts[i] = wrapPromiseWithIndex(iterator.next(), i);\n    }\n\n    const it = wrapWithAbort(this._source, signal)[Symbol.asyncIterator]();\n    iterators[length] = it;\n    nexts[length] = wrapPromiseWithIndex(it.next(), length);\n\n    for (;;) {\n      const next = Promise.race(nexts);\n      const {\n        value: { value: value$, done: done$ },\n        index,\n      } = await next;\n\n      if (index === length) {\n        if (done$) {\n          break;\n        }\n\n        const iterator$ = iterators[index];\n        nexts[index] = wrapPromiseWithIndex(iterator$.next(), index);\n\n        if (hasValueAll) {\n          yield [value$, ...values];\n        }\n      } else if (done$) {\n        nexts[index] = <Promise<MergeResult<IteratorResult<TSource>>>>NEVER_PROMISE;\n      } else {\n        values[index] = value$;\n        hasValue[index] = true;\n        hasValueAll = hasValue.every(identity);\n\n        const iterator$ = iterators[index];\n        nexts[index] = wrapPromiseWithIndex(iterator$.next(), index);\n      }\n    }\n  }\n}\n\n/**\n * Merges multiple async-iterable sequences into one async-iterable sequence by combining each element\n * from the first source with the latest element from the other sources, if any.\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<T2>} source2 Second async-iterable source.\n * @returns {OperatorAsyncFunction<T, [T, T2]>} An async-iterable sequence containing the result of combining\n * each element of the first source with the latest element from the second source, if any as an array.\n */\nexport function withLatestFrom<T, T2>(\n  source2: AsyncIterable<T2>\n): OperatorAsyncFunction<T, [T, T2]>;\n/**\n * Merges multiple async-iterable sequences into one async-iterable sequence by combining each element\n * from the first source with the latest element from the other sources, if any.\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<T2>} source2 Second async-iterable source.\n * @param {AsyncIterable<T3>} source3 Third async-iterable source.\n * @returns {OperatorAsyncFunction<T, [T, T2, T3]>} An async-iterable sequence containing the result of combining\n * each element of the first source with the latest element from the second source, if any as an array.\n */\nexport function withLatestFrom<T, T2, T3>(\n  source2: AsyncIterable<T2>,\n  source3: AsyncIterable<T3>\n): OperatorAsyncFunction<T, [T, T2, T3]>;\n/**\n * Merges multiple async-iterable sequences into one async-iterable sequence by combining each element\n * from the first source with the latest element from the other sources, if any.\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<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 {OperatorAsyncFunction<T, [T, T2, T3, T4]>} An async-iterable sequence containing the result of combining\n * each element of the first source with the latest element from the second source, if any as an array.\n */\nexport function withLatestFrom<T, T2, T3, T4>(\n  source2: AsyncIterable<T2>,\n  source3: AsyncIterable<T3>,\n  source4: AsyncIterable<T4>\n): OperatorAsyncFunction<T, [T, T2, T3, T4]>;\n/**\n * Merges multiple async-iterable sequences into one async-iterable sequence by combining each element\n * from the first source with the latest element from the other sources, if any.\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<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 {OperatorAsyncFunction<T, [T, T2, T3, T4, T5]>} An async-iterable sequence containing the result of combining\n * each element of the first source with the latest element from the second source, if any as an array.\n */\nexport function withLatestFrom<T, T2, T3, T4, T5>(\n  source2: AsyncIterable<T2>,\n  source3: AsyncIterable<T3>,\n  source4: AsyncIterable<T4>,\n  source5: AsyncIterable<T5>\n): OperatorAsyncFunction<T, [T, T2, T3, T4, T5]>;\n/**\n * Merges multiple async-iterable sequences into one async-iterable sequence by combining each element\n * from the first source with the latest element from the other sources, if any.\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<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 {OperatorAsyncFunction<T, [T, T2, T3, T4, T5, T6]>} An async-iterable sequence containing the result of combining\n * each element of the first source with the latest element from the second source, if any as an array.\n */\nexport function withLatestFrom<T, T2, T3, T4, T5, T6>(\n  source2: AsyncIterable<T2>,\n  source3: AsyncIterable<T3>,\n  source4: AsyncIterable<T4>,\n  source5: AsyncIterable<T5>,\n  source6: AsyncIterable<T6>\n): OperatorAsyncFunction<T, [T, T2, T3, T4, T5, T6]>;\n\n/**\n * Merges multiple async-iterable sequences into one async-iterable sequence by combining each element\n * from the first source with the latest element from the other sources, if any.\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 {OperatorAsyncFunction<T, T[]>} An async-iterable sequence containing the result of combining\n * each element of the first source with the latest element from the second source, if any as an array.\n */\nexport function withLatestFrom<T>(...sources: AsyncIterable<T>[]): OperatorAsyncFunction<T, T[]>;\n\nexport function withLatestFrom<T>(...sources: any[]): OperatorAsyncFunction<T, T[]> {\n  return function withLatestFromOperatorFunction(source: AsyncIterable<T>) {\n    return new WithLatestFromAsyncIterable<T>(source, sources);\n  };\n}\n"]}