@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
65 lines (63 loc) • 2.75 kB
JavaScript
import { __asyncValues, __awaiter, __generator } from "tslib";
import { identityAsync } from '../util/identity.js';
import { wrapWithAbort } from './operators/withabort.js';
import { throwIfAborted } from '../aborterror.js';
/**
* Computes the sum of a sequence of values.
*
* @param {AsyncIterable<any>} source A sequence of values to calculate the sum.
* @param {MathOptions<any>} [options] Optional options for providing a selector, thisArg and abort signal.
* @returns {Promise<number>} A promise containing the sum of the sequence of values.
*/
export function sum(source, options) {
var _a, e_1, _b, _c;
return __awaiter(this, void 0, void 0, function () {
var _d, _e, selector, signal, thisArg, value, _f, _g, _h, item, _j, e_1_1;
return __generator(this, function (_k) {
switch (_k.label) {
case 0:
_d = options || {}, _e = _d["selector"], selector = _e === void 0 ? identityAsync : _e, signal = _d["signal"], thisArg = _d["thisArg"];
throwIfAborted(signal);
value = 0;
_k.label = 1;
case 1:
_k.trys.push([1, 7, 8, 13]);
_f = true, _g = __asyncValues(wrapWithAbort(source, signal));
_k.label = 2;
case 2: return [4 /*yield*/, _g.next()];
case 3:
if (!(_h = _k.sent(), _a = _h.done, !_a)) return [3 /*break*/, 6];
_c = _h.value;
_f = false;
item = _c;
_j = value;
return [4 /*yield*/, selector.call(thisArg, item, signal)];
case 4:
value = _j + _k.sent();
_k.label = 5;
case 5:
_f = true;
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 13];
case 7:
e_1_1 = _k.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 13];
case 8:
_k.trys.push([8, , 11, 12]);
if (!(!_f && !_a && (_b = _g.return))) return [3 /*break*/, 10];
return [4 /*yield*/, _b.call(_g)];
case 9:
_k.sent();
_k.label = 10;
case 10: return [3 /*break*/, 12];
case 11:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 12: return [7 /*endfinally*/];
case 13: return [2 /*return*/, value];
}
});
});
}
//# sourceMappingURL=sum.js.map