ix
Version:
The Interactive Extensions for JavaScript
71 lines (69 loc) • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.scan = exports.ScanAsyncIterable = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("../asynciterablex.js");
const withabort_js_1 = require("./withabort.js");
const aborterror_js_1 = require("../../aborterror.js");
/** @ignore */
class ScanAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(source, options) {
super();
this._source = source;
this._fn = options['callback'];
this._hasSeed = options.hasOwnProperty('seed');
this._seed = options['seed'];
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
(0, aborterror_js_1.throwIfAborted)(signal);
let i = 0;
let hasValue = false;
let acc = this._seed;
try {
for (var _e = true, _f = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._source, signal)), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
if (hasValue || (hasValue = this._hasSeed)) {
acc = yield tslib_1.__await(this._fn(acc, item, i++, signal));
yield yield tslib_1.__await(acc);
}
else {
acc = item;
hasValue = true;
i++;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield tslib_1.__await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
if (i === 1 && !this._hasSeed) {
yield yield tslib_1.__await(acc);
}
});
}
}
exports.ScanAsyncIterable = ScanAsyncIterable;
/**
* Applies an accumulator function over an async-iterable sequence and returns each intermediate result.
* The specified seed value, if given, is used as the initial accumulator value.
*
* @template T The type of the elements in the source sequence.
* @template R The type of the result of the aggregation.
* @param {ScanOptions<T, R>} options The options including the accumulator function and seed.
* @returns {OperatorAsyncFunction<T, R>} An async-enumerable sequence containing the accumulated values.
*/
function scan(options) {
return function scanOperatorFunction(source) {
return new ScanAsyncIterable(source, options);
};
}
exports.scan = scan;
//# sourceMappingURL=scan.js.map