ix
Version:
The Interactive Extensions for JavaScript
53 lines (51 loc) • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.whileDo = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("./asynciterablex.js");
const withabort_js_1 = require("./operators/withabort.js");
const aborterror_js_1 = require("../aborterror.js");
class WhileAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(condition, source) {
super();
this._condition = condition;
this._source = source;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
(0, aborterror_js_1.throwIfAborted)(signal);
while (yield tslib_1.__await(this._condition(signal))) {
try {
for (var _e = true, _f = (e_1 = void 0, 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;
yield yield tslib_1.__await(item);
}
}
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; }
}
}
});
}
}
/**
* Repeats the given source as long as the specified conditions holds, where
* the condition is evaluated before each repeated source is iterated.
*
* @template TSource
* @param {AsyncIterable<TSource>} source Source to repeat as long as the condition function evaluates to true.
* @param {((signal?: AbortSignal) => boolean | Promise<boolean>)} condition Condition that will be evaluated before the source sequence is iterated.
* @returns {AsyncIterableX<TSource>} An async-iterable which is repeated while the condition returns true.
*/
function whileDo(source, condition) {
return new WhileAsyncIterable(condition, source);
}
exports.whileDo = whileDo;
//# sourceMappingURL=whiledo.js.map