UNPKG

@rimbu/stream

Version:

Efficient structure representing a sequence of elements, with powerful operations for TypeScript

1,069 lines 50.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncTransformerFastIterator = exports.AsyncReduceIterator = exports.AsyncRepeatIterator = exports.AsyncDropIterator = exports.AsyncTakeIterator = exports.AsyncDropWhileIterator = exports.AsyncCollectIterator = exports.AsyncFilterPureIterator = exports.AsyncFilterIterator = exports.AsyncConcatIterator = exports.AsyncMapPureIterator = exports.AsyncMapIterator = exports.AsyncIndexedIterator = exports.AsyncAppendIterator = exports.AsyncPrependIterator = exports.FromPromise = exports.FromIterator = exports.FromAsyncIterator = exports.AsyncZipAllWithItererator = exports.AsyncZipWithIterator = exports.AsyncUnfoldIterator = exports.FromResourceIterator = exports.AsyncOfIterator = exports.AsyncFastIteratorBase = exports.emptyAsyncFastIterator = exports.fixedDoneAsyncIteratorResult = void 0; exports.isAsyncFastIterator = isAsyncFastIterator; var tslib_1 = require("tslib"); var base_1 = require("@rimbu/base"); var common_1 = require("@rimbu/common"); var async_1 = require("@rimbu/stream/async"); var async_custom_1 = require("@rimbu/stream/async-custom"); exports.fixedDoneAsyncIteratorResult = Object.freeze(Promise.resolve(Object.freeze({ done: true, value: undefined, }))); function isAsyncFastIterator(iterator) { return "fastNext" in iterator; } exports.emptyAsyncFastIterator = Object.freeze({ fastNext: function (otherwise) { return common_1.AsyncOptLazy.toMaybePromise(otherwise); }, next: function () { return exports.fixedDoneAsyncIteratorResult; }, }); var AsyncFastIteratorBase = /** @class */ (function () { function AsyncFastIteratorBase() { } AsyncFastIteratorBase.prototype.next = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, value; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: done = Symbol('Done'); return [4 /*yield*/, this.fastNext(done)]; case 1: value = _a.sent(); if (done === value) return [2 /*return*/, exports.fixedDoneAsyncIteratorResult]; return [2 /*return*/, { value: value, done: false }]; } }); }); }; return AsyncFastIteratorBase; }()); exports.AsyncFastIteratorBase = AsyncFastIteratorBase; var AsyncOfIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncOfIterator, _super); function AsyncOfIterator(values) { var _this = _super.call(this) || this; _this.values = values; _this.index = 0; return _this; } AsyncOfIterator.prototype.fastNext = function (otherwise) { var index = this.index; var values = this.values; if (index >= values.length) return common_1.AsyncOptLazy.toMaybePromise(otherwise); return common_1.AsyncOptLazy.toMaybePromise(values[this.index++]); }; return AsyncOfIterator; }(AsyncFastIteratorBase)); exports.AsyncOfIterator = AsyncOfIterator; var FromResourceIterator = /** @class */ (function (_super) { tslib_1.__extends(FromResourceIterator, _super); function FromResourceIterator(open, createSource, close, asyncStreamSourceHelpers) { var _this = _super.call(this) || this; _this.open = open; _this.createSource = createSource; _this.close = close; _this.asyncStreamSourceHelpers = asyncStreamSourceHelpers; _this.return = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: if (!(close && this.resource)) return [3 /*break*/, 2]; return [4 /*yield*/, close(this.resource)]; case 1: _c.sent(); this.resource = undefined; _c.label = 2; case 2: return [4 /*yield*/, ((_b = (_a = this.iterator) === null || _a === void 0 ? void 0 : _a.return) === null || _b === void 0 ? void 0 : _b.call(_a))]; case 3: _c.sent(); this.return = undefined; return [2 /*return*/]; } }); }); }; return _this; } FromResourceIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var resource, source, err_1; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(undefined === this.iterator)) return [3 /*break*/, 3]; return [4 /*yield*/, this.open()]; case 1: resource = _a.sent(); this.resource = resource; return [4 /*yield*/, this.createSource(resource)]; case 2: source = _a.sent(); this.iterator = this.asyncStreamSourceHelpers .fromAsyncStreamSource(source)[Symbol.asyncIterator](); _a.label = 3; case 3: _a.trys.push([3, 5, , 8]); return [4 /*yield*/, this.iterator.fastNext(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(undefined !== this.return)) return [3 /*break*/, 2]; return [4 /*yield*/, this.return()]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } }); }); })]; case 4: return [2 /*return*/, _a.sent()]; case 5: err_1 = _a.sent(); if (!(undefined !== this.return)) return [3 /*break*/, 7]; return [4 /*yield*/, this.return()]; case 6: _a.sent(); _a.label = 7; case 7: throw err_1; case 8: return [2 /*return*/]; } }); }); }; return FromResourceIterator; }(AsyncFastIteratorBase)); exports.FromResourceIterator = FromResourceIterator; var AsyncUnfoldIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncUnfoldIterator, _super); function AsyncUnfoldIterator(init, getNext) { var _this = _super.call(this) || this; _this.getNext = getNext; _this.index = 0; _this.current = init; return _this; } AsyncUnfoldIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var current, next; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: current = this.current; if (base_1.Token === current) return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; if (this.index === 0) { this.index++; return [2 /*return*/, current]; } return [4 /*yield*/, this.getNext(current, this.index++, base_1.Token)]; case 1: next = _a.sent(); this.current = next; if (base_1.Token === next) return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; return [2 /*return*/, next]; } }); }); }; return AsyncUnfoldIterator; }(AsyncFastIteratorBase)); exports.AsyncUnfoldIterator = AsyncUnfoldIterator; var AsyncZipWithIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncZipWithIterator, _super); function AsyncZipWithIterator(iterables, zipFun, asyncStreamSourceHelpers) { var _this = _super.call(this) || this; _this.iterables = iterables; _this.zipFun = zipFun; _this.asyncStreamSourceHelpers = asyncStreamSourceHelpers; _this.sources = iterables.map(function (source) { return _this.asyncStreamSourceHelpers .fromAsyncStreamSource(source)[Symbol.asyncIterator](); }); _this.sourcesToClose = new Set(_this.sources); _this.return = function () { return async_custom_1.closeIters.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(_this.sourcesToClose), false)); }; return _this; } AsyncZipWithIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var sources, done, result; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: sources = this.sources; done = Symbol('Done'); return [4 /*yield*/, Promise.all(sources.map(function (source) { return source.fastNext(function () { _this.sourcesToClose.delete(source); return done; }); }))]; case 1: result = _a.sent(); if (!(this.sourcesToClose.size !== sources.length)) return [3 /*break*/, 3]; return [4 /*yield*/, (0, async_custom_1.closeIters)(this)]; case 2: _a.sent(); return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; case 3: return [2 /*return*/, this.zipFun.apply(this, tslib_1.__spreadArray([], tslib_1.__read(result), false))]; } }); }); }; return AsyncZipWithIterator; }(AsyncFastIteratorBase)); exports.AsyncZipWithIterator = AsyncZipWithIterator; var AsyncZipAllWithItererator = /** @class */ (function (_super) { tslib_1.__extends(AsyncZipAllWithItererator, _super); function AsyncZipAllWithItererator(fillValue, iters, zipFun, asyncStreamSourceHelpers) { var _this = _super.call(this) || this; _this.fillValue = fillValue; _this.iters = iters; _this.zipFun = zipFun; _this.asyncStreamSourceHelpers = asyncStreamSourceHelpers; _this.sources = iters.map(function (o) { return _this.asyncStreamSourceHelpers .fromAsyncStreamSource(o)[Symbol.asyncIterator](); }); _this.sourcesToClose = new Set(_this.sources); _this.return = function () { return async_custom_1.closeIters.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(_this.sourcesToClose), false)); }; return _this; } AsyncZipAllWithItererator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var sources, fillValue, result; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (this.sourcesToClose.size === 0) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } sources = this.sources; fillValue = this.fillValue; return [4 /*yield*/, Promise.all(sources.map(function (source) { if (_this.sourcesToClose.has(source)) { return source.fastNext(function () { _this.sourcesToClose.delete(source); return common_1.AsyncOptLazy.toMaybePromise(fillValue); }); } return common_1.AsyncOptLazy.toMaybePromise(fillValue); }))]; case 1: result = _a.sent(); if (this.sourcesToClose.size === 0) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } return [2 /*return*/, this.zipFun.apply(this, tslib_1.__spreadArray([], tslib_1.__read(result), false))]; } }); }); }; return AsyncZipAllWithItererator; }(AsyncFastIteratorBase)); exports.AsyncZipAllWithItererator = AsyncZipAllWithItererator; var FromAsyncIterator = /** @class */ (function () { function FromAsyncIterator(source, close) { this.source = source; if (source.return && close) { this.return = function () { var _a; return Promise.all([(_a = source.return) === null || _a === void 0 ? void 0 : _a.call(source), close === null || close === void 0 ? void 0 : close()]); }; } else if (source.return) { this.return = function () { var _a; return (_a = source.return) === null || _a === void 0 ? void 0 : _a.call(source); }; } else if (close) { this.return = close; } } FromAsyncIterator.prototype.next = function () { return this.source.next(); }; FromAsyncIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var result; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.source.next()]; case 1: result = _a.sent(); if (!result.done) return [3 /*break*/, 3]; return [4 /*yield*/, (0, async_custom_1.closeIters)(this)]; case 2: _a.sent(); return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; case 3: return [2 /*return*/, result.value]; } }); }); }; return FromAsyncIterator; }()); exports.FromAsyncIterator = FromAsyncIterator; var FromIterator = /** @class */ (function (_super) { tslib_1.__extends(FromIterator, _super); function FromIterator(iterator, close) { var _this = _super.call(this) || this; _this.iterator = iterator; if (close !== undefined) { _this.return = close; } return _this; } FromIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var result; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: result = this.iterator.next(); if (!result.done) return [3 /*break*/, 2]; return [4 /*yield*/, (0, async_custom_1.closeIters)(this)]; case 1: _a.sent(); return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; case 2: return [2 /*return*/, result.value]; } }); }); }; return FromIterator; }(AsyncFastIteratorBase)); exports.FromIterator = FromIterator; var FromPromise = /** @class */ (function (_super) { tslib_1.__extends(FromPromise, _super); function FromPromise(promise, asyncStreamSourceHelpers, close) { var _this = _super.call(this) || this; _this.promise = promise; _this.asyncStreamSourceHelpers = asyncStreamSourceHelpers; _this.return = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: if (!close) return [3 /*break*/, 2]; return [4 /*yield*/, close()]; case 1: _c.sent(); _c.label = 2; case 2: if (!this.iterator) return [3 /*break*/, 4]; return [4 /*yield*/, ((_b = (_a = this.iterator).return) === null || _b === void 0 ? void 0 : _b.call(_a))]; case 3: _c.sent(); _c.label = 4; case 4: return [2 /*return*/]; } }); }); }; return _this; } FromPromise.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var source; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(this.iterator === undefined)) return [3 /*break*/, 2]; return [4 /*yield*/, this.promise()]; case 1: source = _a.sent(); this.iterator = this.asyncStreamSourceHelpers .fromAsyncStreamSource(source)[Symbol.asyncIterator](); _a.label = 2; case 2: return [2 /*return*/, this.iterator.fastNext(otherwise)]; } }); }); }; return FromPromise; }(AsyncFastIteratorBase)); exports.FromPromise = FromPromise; var AsyncPrependIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncPrependIterator, _super); function AsyncPrependIterator(source, item) { var _this = _super.call(this) || this; _this.source = source; _this.item = item; _this.prependDone = false; _this.return = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { if (this.prependDone) return [2 /*return*/, (0, async_custom_1.closeIters)(this.source)]; return [2 /*return*/]; }); }); }; return _this; } AsyncPrependIterator.prototype.fastNext = function (otherwise) { if (this.prependDone) { return this.source.fastNext(otherwise); } this.prependDone = true; return common_1.AsyncOptLazy.toMaybePromise(this.item); }; return AsyncPrependIterator; }(AsyncFastIteratorBase)); exports.AsyncPrependIterator = AsyncPrependIterator; var AsyncAppendIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncAppendIterator, _super); function AsyncAppendIterator(source, item) { var _this = _super.call(this) || this; _this.source = source; _this.item = item; _this.appendDone = false; _this.return = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { if (!this.appendDone) return [2 /*return*/, (0, async_custom_1.closeIters)(source)]; return [2 /*return*/]; }); }); }; return _this; } AsyncAppendIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, value; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (this.appendDone) return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; done = Symbol('Done'); return [4 /*yield*/, this.source.fastNext(done)]; case 1: value = _a.sent(); if (done !== value) return [2 /*return*/, value]; this.appendDone = true; return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(this.item)]; } }); }); }; return AsyncAppendIterator; }(AsyncFastIteratorBase)); exports.AsyncAppendIterator = AsyncAppendIterator; var AsyncIndexedIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncIndexedIterator, _super); function AsyncIndexedIterator(source, startIndex) { if (startIndex === void 0) { startIndex = 0; } var _this = _super.call(this) || this; _this.source = source; _this.startIndex = startIndex; _this.index = startIndex; _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncIndexedIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, value; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: done = Symbol('Done'); return [4 /*yield*/, this.source.fastNext(done)]; case 1: value = _a.sent(); if (done === value) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } return [2 /*return*/, [this.index++, value]]; } }); }); }; return AsyncIndexedIterator; }(AsyncFastIteratorBase)); exports.AsyncIndexedIterator = AsyncIndexedIterator; var AsyncMapIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncMapIterator, _super); function AsyncMapIterator(source, mapFun) { var _this = _super.call(this) || this; _this.source = source; _this.mapFun = mapFun; _this.state = (0, common_1.TraverseState)(); _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncMapIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var state, done, next; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: state = this.state; done = Symbol('Done'); return [4 /*yield*/, this.source.fastNext(done)]; case 1: next = _a.sent(); if (done === next) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } return [2 /*return*/, this.mapFun(next, state.nextIndex())]; } }); }); }; return AsyncMapIterator; }(AsyncFastIteratorBase)); exports.AsyncMapIterator = AsyncMapIterator; var AsyncMapPureIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncMapPureIterator, _super); function AsyncMapPureIterator(source, mapFun, args) { var _this = _super.call(this) || this; _this.source = source; _this.mapFun = mapFun; _this.args = args; _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncMapPureIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, next; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: done = Symbol('Done'); return [4 /*yield*/, this.source.fastNext(done)]; case 1: next = _a.sent(); if (done === next) return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; return [2 /*return*/, this.mapFun.apply(this, tslib_1.__spreadArray([next], tslib_1.__read(this.args), false))]; } }); }); }; return AsyncMapPureIterator; }(AsyncFastIteratorBase)); exports.AsyncMapPureIterator = AsyncMapPureIterator; var AsyncConcatIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncConcatIterator, _super); function AsyncConcatIterator(source, otherSources, asyncStreamSourceHelpers) { var _this = _super.call(this) || this; _this.source = source; _this.otherSources = otherSources; _this.asyncStreamSourceHelpers = asyncStreamSourceHelpers; _this.sourceIndex = 0; _this.iterator = source[Symbol.asyncIterator](); _this.return = function () { return (0, async_custom_1.closeIters)(_this.iterator); }; return _this; } AsyncConcatIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, value, length, asyncStreamSourceHelpers, _a, nextSource; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: done = Symbol('Done'); length = this.otherSources.length; asyncStreamSourceHelpers = this.asyncStreamSourceHelpers; _b.label = 1; case 1: _a = done; return [4 /*yield*/, this.iterator.fastNext(done)]; case 2: if (!(_a === (value = _b.sent()))) return [3 /*break*/, 3]; if (this.sourceIndex >= length) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } nextSource = this.otherSources[this.sourceIndex++]; while (asyncStreamSourceHelpers.isEmptyAsyncStreamSourceInstance(nextSource)) { if (this.sourceIndex >= length) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } nextSource = this.otherSources[this.sourceIndex++]; } this.iterator = asyncStreamSourceHelpers .fromAsyncStreamSource(nextSource)[Symbol.asyncIterator](); return [3 /*break*/, 1]; case 3: return [2 /*return*/, value]; } }); }); }; return AsyncConcatIterator; }(AsyncFastIteratorBase)); exports.AsyncConcatIterator = AsyncConcatIterator; var AsyncFilterIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncFilterIterator, _super); function AsyncFilterIterator(source, pred, negate) { var _this = _super.call(this) || this; _this.source = source; _this.pred = pred; _this.negate = negate; _this.state = (0, common_1.TraverseState)(); _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncFilterIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var state, done, value, source, pred, halt, negate, _a, _b, cond; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: state = this.state; if (state.halted) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } done = Symbol('Done'); source = this.source; pred = this.pred; halt = state.halt; negate = this.negate; _c.label = 1; case 1: _a = !state.halted; if (!_a) return [3 /*break*/, 3]; _b = done; return [4 /*yield*/, source.fastNext(done)]; case 2: _a = _b !== (value = _c.sent()); _c.label = 3; case 3: if (!_a) return [3 /*break*/, 5]; return [4 /*yield*/, pred(value, state.nextIndex(), halt)]; case 4: cond = _c.sent(); if (cond !== negate) return [2 /*return*/, value]; return [3 /*break*/, 1]; case 5: if (!(state.halted && done !== value)) return [3 /*break*/, 7]; return [4 /*yield*/, (0, async_custom_1.closeIters)(this)]; case 6: _c.sent(); _c.label = 7; case 7: return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } }); }); }; return AsyncFilterIterator; }(AsyncFastIteratorBase)); exports.AsyncFilterIterator = AsyncFilterIterator; var AsyncFilterPureIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncFilterPureIterator, _super); function AsyncFilterPureIterator(source, pred, args, negate) { var _this = _super.call(this) || this; _this.source = source; _this.pred = pred; _this.args = args; _this.negate = negate; _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncFilterPureIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, value, source, pred, args, negate, _a, cond; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: done = Symbol('Done'); source = this.source; pred = this.pred; args = this.args; negate = this.negate; _b.label = 1; case 1: _a = done; return [4 /*yield*/, source.fastNext(done)]; case 2: if (!(_a !== (value = _b.sent()))) return [3 /*break*/, 4]; return [4 /*yield*/, pred.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false))]; case 3: cond = _b.sent(); if (cond !== negate) return [2 /*return*/, value]; return [3 /*break*/, 1]; case 4: return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } }); }); }; return AsyncFilterPureIterator; }(AsyncFastIteratorBase)); exports.AsyncFilterPureIterator = AsyncFilterPureIterator; var AsyncCollectIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncCollectIterator, _super); function AsyncCollectIterator(source, collectFun) { var _this = _super.call(this) || this; _this.source = source; _this.collectFun = collectFun; _this.state = (0, common_1.TraverseState)(); _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncCollectIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var state, halt, done, value, source, collectFun, _a, _b, result; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: state = this.state; if (state.halted) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } halt = state.halt; done = Symbol('Done'); source = this.source; collectFun = this.collectFun; _c.label = 1; case 1: _c.trys.push([1, , 7, 10]); _c.label = 2; case 2: _a = !state.halted; if (!_a) return [3 /*break*/, 4]; _b = done; return [4 /*yield*/, source.fastNext(done)]; case 3: _a = _b !== (value = _c.sent()); _c.label = 4; case 4: if (!_a) return [3 /*break*/, 6]; return [4 /*yield*/, collectFun(value, state.nextIndex(), common_1.CollectFun.Skip, halt)]; case 5: result = _c.sent(); if (common_1.CollectFun.Skip === result) return [3 /*break*/, 2]; return [2 /*return*/, result]; case 6: return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; case 7: if (!(state.halted && done !== value)) return [3 /*break*/, 9]; return [4 /*yield*/, (0, async_custom_1.closeIters)(this)]; case 8: _c.sent(); _c.label = 9; case 9: return [7 /*endfinally*/]; case 10: return [2 /*return*/]; } }); }); }; return AsyncCollectIterator; }(AsyncFastIteratorBase)); exports.AsyncCollectIterator = AsyncCollectIterator; var AsyncDropWhileIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncDropWhileIterator, _super); function AsyncDropWhileIterator(source, pred, negate) { var _this = _super.call(this) || this; _this.source = source; _this.pred = pred; _this.negate = negate; _this.pass = false; _this.index = 0; _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncDropWhileIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var source, done, value, negate, _a, _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: source = this.source; if (this.pass) return [2 /*return*/, source.fastNext(otherwise)]; done = Symbol('Done'); negate = this.negate; _c.label = 1; case 1: _a = done; return [4 /*yield*/, source.fastNext(done)]; case 2: if (!(_a !== (value = _c.sent()))) return [3 /*break*/, 4]; _b = this; return [4 /*yield*/, this.pred(value, this.index++)]; case 3: _b.pass = (_c.sent()) === negate; if (this.pass) return [2 /*return*/, value]; return [3 /*break*/, 1]; case 4: return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } }); }); }; return AsyncDropWhileIterator; }(AsyncFastIteratorBase)); exports.AsyncDropWhileIterator = AsyncDropWhileIterator; var AsyncTakeIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncTakeIterator, _super); function AsyncTakeIterator(source, amount) { var _this = _super.call(this) || this; _this.source = source; _this.amount = amount; _this.i = 0; _this.return = function () { return (0, async_custom_1.closeIters)(source); }; return _this; } AsyncTakeIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(this.i++ >= this.amount)) return [3 /*break*/, 2]; return [4 /*yield*/, (0, async_custom_1.closeIters)(this)]; case 1: _a.sent(); this.return = undefined; return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; case 2: return [2 /*return*/, this.source.fastNext(otherwise)]; } }); }); }; return AsyncTakeIterator; }(AsyncFastIteratorBase)); exports.AsyncTakeIterator = AsyncTakeIterator; var AsyncDropIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncDropIterator, _super); function AsyncDropIterator(source, amount) { var _this = _super.call(this) || this; _this.source = source; _this.amount = amount; _this.return = function () { return (0, async_custom_1.closeIters)(source); }; _this.remain = amount; return _this; } AsyncDropIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var source, done, value, _a; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: source = this.source; if (this.remain <= 0) return [2 /*return*/, source.fastNext(otherwise)]; done = Symbol('Done'); _b.label = 1; case 1: _a = done; return [4 /*yield*/, source.fastNext(done)]; case 2: if (!(_a !== (value = _b.sent()))) return [3 /*break*/, 3]; if (this.remain-- <= 0) { return [2 /*return*/, value]; } return [3 /*break*/, 1]; case 3: return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } }); }); }; return AsyncDropIterator; }(AsyncFastIteratorBase)); exports.AsyncDropIterator = AsyncDropIterator; var AsyncRepeatIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncRepeatIterator, _super); function AsyncRepeatIterator(source, amount) { var _this = _super.call(this) || this; _this.source = source; _this.amount = amount; _this.isEmpty = true; _this.iterator = source[Symbol.asyncIterator](); _this.return = function () { return (0, async_custom_1.closeIters)(_this.iterator); }; _this.remain = amount; return _this; } AsyncRepeatIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var done, iterator, value; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: done = Symbol('Done'); iterator = this.iterator; return [4 /*yield*/, iterator.fastNext(done)]; case 1: value = _a.sent(); if (done !== value) { this.isEmpty = false; return [2 /*return*/, value]; } if (this.isEmpty) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } if (undefined !== this.remain) { this.remain--; if (this.remain <= 0) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } } this.iterator = this.source[Symbol.asyncIterator](); return [4 /*yield*/, this.iterator.fastNext(done)]; case 2: value = _a.sent(); if (done === value) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } return [2 /*return*/, value]; } }); }); }; return AsyncRepeatIterator; }(AsyncFastIteratorBase)); exports.AsyncRepeatIterator = AsyncRepeatIterator; var AsyncReduceIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncReduceIterator, _super); function AsyncReduceIterator(sourceIterator, reducer) { var _this = _super.call(this) || this; _this.sourceIterator = sourceIterator; _this.reducer = reducer; _this.return = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(undefined !== this.___instance && !this.___instance.halted)) return [3 /*break*/, 2]; return [4 /*yield*/, Promise.all([ (0, async_custom_1.closeIters)(sourceIterator), this.___instance.onClose(), ])]; case 1: _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, (0, async_custom_1.closeIters)(sourceIterator)]; case 3: _a.sent(); _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; return _this; } AsyncReduceIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, reducerInstance, done, nextInput; var _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: if (!(undefined === this.___instance)) return [3 /*break*/, 2]; _a = this; return [4 /*yield*/, this.reducer.compile()]; case 1: _a.___instance = _c.sent(); _c.label = 2; case 2: reducerInstance = this.___instance; _c.label = 3; case 3: _c.trys.push([3, , 8, 9]); if (!reducerInstance.halted) return [3 /*break*/, 5]; return [4 /*yield*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; case 4: return [2 /*return*/, (_c.sent())]; case 5: done = Symbol('Done'); return [4 /*yield*/, this.sourceIterator.fastNext(done)]; case 6: nextInput = _c.sent(); if (done === nextInput) { this.___instance.halt(); return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } return [4 /*yield*/, reducerInstance.next(nextInput)]; case 7: _c.sent(); return [2 /*return*/, reducerInstance.getOutput()]; case 8: if (reducerInstance.halted) { (_b = this.return) === null || _b === void 0 ? void 0 : _b.call(this); this.return = undefined; } return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); }; return AsyncReduceIterator; }(AsyncFastIteratorBase)); exports.AsyncReduceIterator = AsyncReduceIterator; var AsyncTransformerFastIterator = /** @class */ (function (_super) { tslib_1.__extends(AsyncTransformerFastIterator, _super); function AsyncTransformerFastIterator(sourceIterator, transformer) { var _this = _super.call(this) || this; _this.sourceIterator = sourceIterator; _this.transformer = transformer; _this.___done = false; _this.return = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!(undefined !== this.___instance)) return [3 /*break*/, 2]; return [4 /*yield*/, Promise.all([ (0, async_custom_1.closeIters)(sourceIterator), this.___instance.onClose(), ])]; case 1: _a.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, (0, async_custom_1.closeIters)(sourceIterator)]; case 3: _a.sent(); _a.label = 4; case 4: return [2 /*return*/]; } }); }); }; return _this; } AsyncTransformerFastIterator.prototype.fastNext = function (otherwise) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, transformerInstance, done, nextValue, _b, _c, nextSource, nextValuesSource; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: if (this.___done) { return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)]; } if (!(undefined === this.___instance)) return [3 /*break*/, 2]; _a = this; return [4 /*yield*/, async_1.AsyncTransformer.from(this.transformer).compile()]; case 1: _a.___instance = _d.sent(); _d.label = 2; case 2: transformerInstance = this.___instance; done = Symbol('done'); _d.label = 3; case 3: _b = undefined === this.___currentValues; if (_b) return [3 /*break*/, 5]; _c = done; return [4 /*yield*/, this.___currentValues.fastNext(done)]; case 4: _b = _c === (nextValue = _d.sent()); _d.label = 5; case 5: if (!_b) return [3 /*break*/, 11]; return [4 /*yield*/, this.sourceIterator.fastNext(done)]; case 6: nextSource = _d.sent(); if (!(done === nextSource)) return [3 /*break*/, 7]; if (transformerInstance.halted) { return [2 /*return*/, common_1.AsyncOptLazy.toMaybePromise(otherwise)]; } this.___done = true; transformerInstance.halt(); this.return = undefined; return [3 /*break*/, 9]; case 7: return [4 /*yield*/, transformerInstance.next(nextSource)]; case 8: _d.sent(); _d.label = 9; case 9: return [4 /*yield*/, transformerIn