@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
103 lines (101 loc) • 4.52 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await, __extends, __generator } from "tslib";
import { AsyncIterableX } from '../asynciterablex.js';
import { wrapWithAbort } from './withabort.js';
import { throwIfAborted } from '../../aborterror.js';
/** @ignore */
var ScanAsyncIterable = /** @class */ (function (_super) {
__extends(ScanAsyncIterable, _super);
function ScanAsyncIterable(source, options) {
var _this = _super.call(this) || this;
_this._source = source;
_this._fn = options['callback'];
_this._hasSeed = options.hasOwnProperty('seed');
_this._seed = options['seed'];
return _this;
}
ScanAsyncIterable.prototype[Symbol.asyncIterator] = function (signal) {
return __asyncGenerator(this, arguments, function _a() {
var i, hasValue, acc, _b, _c, _d, item, e_1_1;
var _e, e_1, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
throwIfAborted(signal);
i = 0;
hasValue = false;
acc = this._seed;
_h.label = 1;
case 1:
_h.trys.push([1, 10, 11, 16]);
_b = true, _c = __asyncValues(wrapWithAbort(this._source, signal));
_h.label = 2;
case 2: return [4 /*yield*/, __await(_c.next())];
case 3:
if (!(_d = _h.sent(), _e = _d.done, !_e)) return [3 /*break*/, 9];
_g = _d.value;
_b = false;
item = _g;
if (!(hasValue || (hasValue = this._hasSeed))) return [3 /*break*/, 7];
return [4 /*yield*/, __await(this._fn(acc, item, i++, signal))];
case 4:
acc = _h.sent();
return [4 /*yield*/, __await(acc)];
case 5: return [4 /*yield*/, _h.sent()];
case 6:
_h.sent();
return [3 /*break*/, 8];
case 7:
acc = item;
hasValue = true;
i++;
_h.label = 8;
case 8:
_b = true;
return [3 /*break*/, 2];
case 9: return [3 /*break*/, 16];
case 10:
e_1_1 = _h.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 16];
case 11:
_h.trys.push([11, , 14, 15]);
if (!(!_b && !_e && (_f = _c.return))) return [3 /*break*/, 13];
return [4 /*yield*/, __await(_f.call(_c))];
case 12:
_h.sent();
_h.label = 13;
case 13: return [3 /*break*/, 15];
case 14:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 15: return [7 /*endfinally*/];
case 16:
if (!(i === 1 && !this._hasSeed)) return [3 /*break*/, 19];
return [4 /*yield*/, __await(acc)];
case 17: return [4 /*yield*/, _h.sent()];
case 18:
_h.sent();
_h.label = 19;
case 19: return [2 /*return*/];
}
});
});
};
return ScanAsyncIterable;
}(AsyncIterableX));
export { 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.
*/
export function scan(options) {
return function scanOperatorFunction(source) {
return new ScanAsyncIterable(source, options);
};
}
//# sourceMappingURL=scan.js.map