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AAE,CAAC;aACL;SACF;QACD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAC7B,MAAM,GAAQ,CAAC;SAChB;IACH,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,IAAI,CAAW,OAA0B;IACvD,OAAO,SAAS,oBAAoB,CAAC,MAAwB;QAC3D,OAAO,IAAI,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChD,CAAC,CAAC;AACJ,CAAC","file":"scan.js","sourcesContent":["import { AsyncIterableX } from '../asynciterablex';\nimport { OperatorAsyncFunction } from '../../interfaces';\nimport { wrapWithAbort } from './withabort';\nimport { ScanOptions } from './scanoptions';\nimport { throwIfAborted } from '../../aborterror';\n\nexport class ScanAsyncIterable<T, R> extends AsyncIterableX<R> {\n private _source: AsyncIterable<T>;\n private _fn: (acc: R, x: T, index: number, signal?: AbortSignal) => R | Promise<R>;\n private _seed?: T | R;\n private _hasSeed: boolean;\n\n constructor(source: AsyncIterable<T>, options: ScanOptions<T, R>) {\n super();\n this._source = source;\n this._fn = options['callback'];\n this._hasSeed = options.hasOwnProperty('seed');\n this._seed = options['seed'];\n }\n\n async *[Symbol.asyncIterator](signal?: AbortSignal) {\n throwIfAborted(signal);\n let i = 0;\n let hasValue = false;\n let acc = this._seed;\n for await (const item of wrapWithAbort(this._source, signal)) {\n if (hasValue || (hasValue = this._hasSeed)) {\n acc = await this._fn(<R>acc, item, i++, signal);\n yield acc;\n } else {\n acc = item;\n hasValue = true;\n i++;\n }\n }\n if (i === 1 && !this._hasSeed) {\n yield acc as R;\n }\n }\n}\n\n/**\n * Applies an accumulator function over an async-iterable sequence and returns each intermediate result.\n * The specified seed value, if given, is used as the initial accumulator value.\n *\n * @export\n * @template T The type of the elements in the source sequence.\n * @template R The type of the result of the aggregation.\n * @param {ScanOptions<T, R>} options The options including the accumulator function and seed.\n * @returns {OperatorAsyncFunction<T, R>} An async-enumerable sequence containing the accumulated values.\n */\nexport function scan<T, R = T>(options: ScanOptions<T, R>): OperatorAsyncFunction<T, R> {\n return function scanOperatorFunction(source: AsyncIterable<T>): AsyncIterableX<R> {\n return new ScanAsyncIterable(source, options);\n };\n}\n"]}