@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
37 lines (35 loc) • 1.02 kB
JavaScript
import { AsyncIterableX } from '../asynciterablex';
export class ScanAsyncIterable extends AsyncIterableX {
constructor(source, fn, seed) {
super();
this._source = source;
this._fn = fn;
this._hasSeed = seed.length === 1;
this._seed = seed[0];
}
async *[Symbol.asyncIterator]() {
let i = 0;
let hasValue = false;
let acc = this._seed;
for await (const item of this._source) {
if (hasValue || (hasValue = this._hasSeed)) {
acc = await this._fn(acc, item, i++);
yield acc;
}
else {
acc = item;
hasValue = true;
i++;
}
}
if (i === 1 && !this._hasSeed) {
yield acc;
}
}
}
export function scan(accumulator, ...seed) {
return function scanOperatorFunction(source) {
return new ScanAsyncIterable(source, accumulator, seed);
};
}
//# sourceMappingURL=scan.mjs.map