@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
83 lines (81 loc) • 3.76 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await, __extends, __generator } from "tslib";
import { AsyncIterableX } from './asynciterablex.js';
import { wrapWithAbort } from './operators/withabort.js';
import { throwIfAborted } from '../aborterror.js';
var WhileAsyncIterable = /** @class */ (function (_super) {
__extends(WhileAsyncIterable, _super);
function WhileAsyncIterable(condition, source) {
var _this = _super.call(this) || this;
_this._condition = condition;
_this._source = source;
return _this;
}
WhileAsyncIterable.prototype[Symbol.asyncIterator] = function (signal) {
return __asyncGenerator(this, arguments, function _a() {
var _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);
_h.label = 1;
case 1: return [4 /*yield*/, __await(this._condition(signal))];
case 2:
if (!_h.sent()) return [3 /*break*/, 17];
_h.label = 3;
case 3:
_h.trys.push([3, 10, 11, 16]);
_b = true, _c = (e_1 = void 0, __asyncValues(wrapWithAbort(this._source, signal)));
_h.label = 4;
case 4: return [4 /*yield*/, __await(_c.next())];
case 5:
if (!(_d = _h.sent(), _e = _d.done, !_e)) return [3 /*break*/, 9];
_g = _d.value;
_b = false;
item = _g;
return [4 /*yield*/, __await(item)];
case 6: return [4 /*yield*/, _h.sent()];
case 7:
_h.sent();
_h.label = 8;
case 8:
_b = true;
return [3 /*break*/, 4];
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: return [3 /*break*/, 1];
case 17: return [2 /*return*/];
}
});
});
};
return WhileAsyncIterable;
}(AsyncIterableX));
/**
* 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.
*/
export function whileDo(source, condition) {
return new WhileAsyncIterable(condition, source);
}
//# sourceMappingURL=whiledo.js.map