@rimbu/stream
Version:
Efficient structure representing a sequence of elements, with powerful operations for TypeScript
1,087 lines • 107 kB
JavaScript
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncStreamConstructorsImpl = exports.fromAsyncStreamSource = exports.emptyAsyncStream = exports.FromResource = exports.FromSource = exports.AsyncOfStream = exports.AsyncTransformerStream = exports.AsyncFromStream = exports.AsyncStreamBase = void 0;
exports.isAsyncStream = isAsyncStream;
exports.isEmptyAsyncStreamSourceInstance = isEmptyAsyncStreamSourceInstance;
exports.asyncStreamSourceToIterator = asyncStreamSourceToIterator;
var tslib_1 = require("tslib");
var base_1 = require("@rimbu/base");
var common_1 = require("@rimbu/common");
var stream_1 = require("@rimbu/stream");
var async_1 = require("@rimbu/stream/async");
var async_custom_1 = require("@rimbu/stream/async-custom");
var custom_1 = require("@rimbu/stream/custom");
var AsyncStreamBase = /** @class */ (function () {
function AsyncStreamBase() {
}
AsyncStreamBase.prototype.asyncStream = function () {
return this;
};
AsyncStreamBase.prototype.equals = function (other_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (other, options) {
var _b, eq, _c, negate, it1, it2, done, _d, v1, v2;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_b = options.eq, eq = _b === void 0 ? common_1.Eq.objectIs : _b, _c = options.negate, negate = _c === void 0 ? false : _c;
it1 = this[Symbol.asyncIterator]();
it2 = (0, exports.fromAsyncStreamSource)(other)[Symbol.asyncIterator]();
done = Symbol('Done');
_e.label = 1;
case 1:
if (!true) return [3 /*break*/, 9];
return [4 /*yield*/, Promise.all([
it1.fastNext(done),
it2.fastNext(done),
])];
case 2:
_d = tslib_1.__read.apply(void 0, [_e.sent(), 2]), v1 = _d[0], v2 = _d[1];
if (!(done === v1)) return [3 /*break*/, 4];
if (done === v2)
return [2 /*return*/, true];
return [4 /*yield*/, (0, async_custom_1.closeIters)(it2)];
case 3:
_e.sent();
return [2 /*return*/, false];
case 4:
if (!(done === v2)) return [3 /*break*/, 6];
return [4 /*yield*/, (0, async_custom_1.closeIters)(it1)];
case 5:
_e.sent();
return [2 /*return*/, false];
case 6:
if (!(eq(v1, v2) === negate)) return [3 /*break*/, 8];
return [4 /*yield*/, (0, async_custom_1.closeIters)(it1, it2)];
case 7:
_e.sent();
return [2 /*return*/, false];
case 8: return [3 /*break*/, 1];
case 9: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.assumeNonEmpty = function () {
return this;
};
AsyncStreamBase.prototype.asNormal = function () {
return this;
};
AsyncStreamBase.prototype.prepend = function (value) {
return new AsyncPrependStream(this, value).assumeNonEmpty();
};
AsyncStreamBase.prototype.append = function (value) {
return new AsyncAppendStream(this, value).assumeNonEmpty();
};
AsyncStreamBase.prototype.forEach = function (f_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (f, options) {
var _b, state, done, value, iterator, halt, _c, _d;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_b = options.state, state = _b === void 0 ? (0, common_1.TraverseState)() : _b;
if (state.halted)
return [2 /*return*/];
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
halt = state.halt;
_e.label = 1;
case 1:
_e.trys.push([1, , 7, 10]);
_e.label = 2;
case 2:
_c = !state.halted;
if (!_c) return [3 /*break*/, 4];
_d = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
_c = _d !== (value = _e.sent());
_e.label = 4;
case 4:
if (!_c) return [3 /*break*/, 6];
return [4 /*yield*/, f(value, state.nextIndex(), halt)];
case 5:
_e.sent();
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 10];
case 7:
if (!(done !== value)) return [3 /*break*/, 9];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 8:
_e.sent();
_e.label = 9;
case 9: return [7 /*endfinally*/];
case 10: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.forEachPure = function (f) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return tslib_1.__awaiter(this, void 0, void 0, function () {
var done, value, iterator, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
_c.label = 1;
case 1:
_c.trys.push([1, , 6, 9]);
_c.label = 2;
case 2:
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_b !== (value = _c.sent()))) return [3 /*break*/, 5];
return [4 /*yield*/, f.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false))];
case 4:
_c.sent();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 9];
case 6:
if (!(done !== value)) return [3 /*break*/, 8];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_c.sent();
_c.label = 8;
case 8: return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.indexed = function (options) {
if (options === void 0) { options = {}; }
var _b = options.startIndex, startIndex = _b === void 0 ? 0 : _b;
return new AsyncIndexedStream(this, startIndex);
};
AsyncStreamBase.prototype.filter = function (pred, options) {
if (options === void 0) { options = {}; }
var _b = options.negate, negate = _b === void 0 ? false : _b;
return new AsyncFilterStream(this, pred, negate);
};
AsyncStreamBase.prototype.filterPure = function (options) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var pred = options.pred, _b = options.negate, negate = _b === void 0 ? false : _b;
return new AsyncFilterPureStream(this, pred, args, negate);
};
AsyncStreamBase.prototype.withOnly = function (values) {
if (values.length <= 0) {
return this;
}
var set = new Set(values);
return this.filterPure({ pred: function (v) { return set.has(v); } });
};
AsyncStreamBase.prototype.without = function (values) {
if (values.length <= 0) {
return this;
}
var set = new Set(values);
return this.filterPure({ pred: function (v) { return set.has(v); }, negate: true });
};
AsyncStreamBase.prototype.map = function (mapFun) {
return new AsyncMapStream(this, mapFun);
};
AsyncStreamBase.prototype.mapPure = function (mapFun) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return new AsyncMapPureStream(this, mapFun, args);
};
AsyncStreamBase.prototype.collect = function (collectFun) {
return new AsyncCollectStream(this, collectFun);
};
AsyncStreamBase.prototype.flatMap = function (flatMapFun) {
return this.transform(async_1.AsyncTransformer.flatMap(flatMapFun));
};
AsyncStreamBase.prototype.flatZip = function (flatMapFun) {
return this.transform(async_1.AsyncTransformer.flatZip(flatMapFun));
};
AsyncStreamBase.prototype.transform = function (transformer) {
return new AsyncTransformerStream(this, transformer);
};
AsyncStreamBase.prototype.first = function (otherwise) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var done, iter, value;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
done = Symbol('done');
iter = this[Symbol.asyncIterator]();
value = done;
_b.label = 1;
case 1:
_b.trys.push([1, , 3, 6]);
return [4 /*yield*/, iter.fastNext(done)];
case 2:
value = _b.sent();
if (done === value) {
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
}
return [2 /*return*/, value];
case 3:
if (!(done !== value)) return [3 /*break*/, 5];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iter)];
case 4:
_b.sent();
_b.label = 5;
case 5: return [7 /*endfinally*/];
case 6: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.last = function (otherwise) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var done, iterator, value, lastValue, _b, err_1;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
_c.label = 1;
case 1:
_c.trys.push([1, 5, , 7]);
value = done;
lastValue = done;
_c.label = 2;
case 2:
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_b !== (value = _c.sent()))) return [3 /*break*/, 4];
lastValue = value;
return [3 /*break*/, 2];
case 4:
if (done === lastValue) {
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
}
return [2 /*return*/, lastValue];
case 5:
err_1 = _c.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 6:
_c.sent();
throw err_1;
case 7: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.single = function (otherwise) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var iterator, done, value, resultValue, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
iterator = this[Symbol.asyncIterator]();
done = Symbol('Done');
value = done;
_c.label = 1;
case 1:
_c.trys.push([1, , 5, 8]);
return [4 /*yield*/, iterator.fastNext(done)];
case 2:
value = _c.sent();
if (!(done !== value)) return [3 /*break*/, 4];
resultValue = value;
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (_b === (value = _c.sent())) {
return [2 /*return*/, resultValue];
}
_c.label = 4;
case 4: return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
case 5:
if (!(done !== value)) return [3 /*break*/, 7];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 6:
_c.sent();
_c.label = 7;
case 7: return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.count = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, done, iterator, _b, err_2;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
result = 0;
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
_c.label = 1;
case 1:
_c.trys.push([1, 5, , 7]);
_c.label = 2;
case 2:
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_b !== (_c.sent()))) return [3 /*break*/, 4];
result++;
return [3 /*break*/, 2];
case 4: return [2 /*return*/, result];
case 5:
err_2 = _c.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 6:
_c.sent();
throw err_2;
case 7: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.countElement = function (value_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (value, options) {
var _b, eq, _c, negate, done, iterator, result, current, _d, err_3;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_b = options.eq, eq = _b === void 0 ? common_1.Eq.objectIs : _b, _c = options.negate, negate = _c === void 0 ? false : _c;
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
_e.label = 1;
case 1:
_e.trys.push([1, 5, , 7]);
result = 0;
current = void 0;
_e.label = 2;
case 2:
_d = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_d !== (current = _e.sent()))) return [3 /*break*/, 4];
if (eq(value, current) !== negate) {
result++;
}
return [3 /*break*/, 2];
case 4: return [2 /*return*/, result];
case 5:
err_3 = _e.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 6:
_e.sent();
throw err_3;
case 7: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.find = function (pred_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (pred, options) {
var _b, occurrance, _c, negate, otherwise, done, iterator, value, remain, index, _d, cond;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_b = options.occurrance, occurrance = _b === void 0 ? 1 : _b, _c = options.negate, negate = _c === void 0 ? false : _c, otherwise = options.otherwise;
if (occurrance <= 0)
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
remain = occurrance;
index = 0;
_e.label = 1;
case 1:
_e.trys.push([1, , 6, 9]);
_e.label = 2;
case 2:
_d = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_d !== (value = _e.sent()))) return [3 /*break*/, 5];
return [4 /*yield*/, pred(value, index++)];
case 4:
cond = _e.sent();
if (cond !== negate && --remain <= 0) {
return [2 /*return*/, value];
}
return [3 /*break*/, 2];
case 5: return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
case 6:
if (!(done !== value)) return [3 /*break*/, 8];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_e.sent();
_e.label = 8;
case 8: return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.elementAt = function (index, otherwise) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var done, iterator, value, i, _b, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
if (index < 0)
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
i = 0;
_d.label = 1;
case 1:
_d.trys.push([1, , 6, 9]);
_d.label = 2;
case 2:
_b = i <= index;
if (!_b) return [3 /*break*/, 4];
_c = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
_b = _c !== (value = _d.sent());
_d.label = 4;
case 4:
if (!_b) return [3 /*break*/, 5];
if (i === index) {
return [2 /*return*/, value];
}
i++;
return [3 /*break*/, 2];
case 5: return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
case 6:
if (!(done !== value)) return [3 /*break*/, 8];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_d.sent();
_d.label = 8;
case 8: return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.indicesWhere = function (pred, options) {
if (options === void 0) { options = {}; }
return this.transform(async_1.AsyncTransformer.indicesWhere(pred, options));
};
AsyncStreamBase.prototype.indicesOf = function (searchValue, options) {
if (options === void 0) { options = {}; }
return this.transform(stream_1.Transformer.indicesOf(searchValue, options));
};
AsyncStreamBase.prototype.indexWhere = function (pred_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (pred, options) {
var _b, occurrance, _c, negate, done, value, iterator, index, occ, _d, i, cond;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_b = options.occurrance, occurrance = _b === void 0 ? 1 : _b, _c = options.negate, negate = _c === void 0 ? false : _c;
if (occurrance <= 0) {
return [2 /*return*/, undefined];
}
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
index = 0;
occ = 0;
_e.label = 1;
case 1:
_e.trys.push([1, , 6, 9]);
_e.label = 2;
case 2:
_d = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_d !== (value = _e.sent()))) return [3 /*break*/, 5];
i = index++;
return [4 /*yield*/, pred(value, i)];
case 4:
cond = _e.sent();
if (cond === !negate) {
occ++;
if (occ >= occurrance) {
return [2 /*return*/, i];
}
}
return [3 /*break*/, 2];
case 5: return [2 /*return*/, undefined];
case 6:
if (!(done !== value)) return [3 /*break*/, 8];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_e.sent();
_e.label = 8;
case 8: return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.indexOf = function (searchValue_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (searchValue, options) {
var _b, occurrance, _c, eq, _d, negate, done, value, iterator, index, occ, _e, i;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_f) {
switch (_f.label) {
case 0:
_b = options.occurrance, occurrance = _b === void 0 ? 1 : _b, _c = options.eq, eq = _c === void 0 ? common_1.Eq.objectIs : _c, _d = options.negate, negate = _d === void 0 ? false : _d;
if (occurrance <= 0)
return [2 /*return*/, undefined];
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
index = 0;
occ = 0;
_f.label = 1;
case 1:
_f.trys.push([1, , 5, 8]);
_f.label = 2;
case 2:
_e = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_e !== (value = _f.sent()))) return [3 /*break*/, 4];
i = index++;
if (eq(value, searchValue) !== negate) {
occ++;
if (occ >= occurrance) {
return [2 /*return*/, i];
}
}
return [3 /*break*/, 2];
case 4: return [2 /*return*/, undefined];
case 5:
if (!(done !== value)) return [3 /*break*/, 7];
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 6:
_f.sent();
_f.label = 7;
case 7: return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.some = function (pred_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (pred, options) {
var _b;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = undefined;
return [4 /*yield*/, this.indexWhere(pred, options)];
case 1: return [2 /*return*/, _b !== (_c.sent())];
}
});
});
};
AsyncStreamBase.prototype.every = function (pred_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (pred, options) {
var _b, negate, _c;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_b = options.negate, negate = _b === void 0 ? false : _b;
_c = undefined;
return [4 /*yield*/, this.indexWhere(pred, { negate: !negate })];
case 1: return [2 /*return*/, _c === (_d.sent())];
}
});
});
};
AsyncStreamBase.prototype.contains = function (searchValue_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (searchValue, options) {
var _b, amount, _c, eq, _d, negate, _e;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_f) {
switch (_f.label) {
case 0:
_b = options.amount, amount = _b === void 0 ? 1 : _b;
if (amount <= 0) {
return [2 /*return*/, true];
}
_c = options.eq, eq = _c === void 0 ? common_1.Eq.objectIs : _c, _d = options.negate, negate = _d === void 0 ? false : _d;
_e = undefined;
return [4 /*yield*/, this.indexOf(searchValue, { occurrance: amount, eq: eq, negate: negate })];
case 1: return [2 /*return*/, (_e !==
(_f.sent()))];
}
});
});
};
AsyncStreamBase.prototype.containsSlice = function (source_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (source, options) {
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
return [2 /*return*/, this.reduce(async_1.AsyncReducer.containsSlice(source, options))];
});
});
};
AsyncStreamBase.prototype.takeWhile = function (pred, options) {
var _this = this;
if (options === void 0) { options = {}; }
var _b = options.negate, negate = _b === void 0 ? false : _b;
return this.filter(function (value, index, halt) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var result;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, pred(value, index)];
case 1:
result = (_b.sent()) !== negate;
if (!result) {
halt();
}
return [2 /*return*/, result];
}
});
}); });
};
AsyncStreamBase.prototype.dropWhile = function (pred, options) {
if (options === void 0) { options = {}; }
var _b = options.negate, negate = _b === void 0 ? false : _b;
return new AsyncDropWhileStream(this, pred, negate);
};
AsyncStreamBase.prototype.take = function (amount) {
if (amount <= 0) {
return exports.emptyAsyncStream;
}
return new AsyncTakeStream(this, amount);
};
AsyncStreamBase.prototype.drop = function (amount) {
if (amount <= 0) {
return this;
}
return new AsyncDropStream(this, amount);
};
AsyncStreamBase.prototype.repeat = function (amount) {
var _this = this;
if (undefined !== amount && amount <= 1) {
return this;
}
return new AsyncFromStream(function () { return new async_custom_1.AsyncRepeatIterator(_this, amount); });
};
AsyncStreamBase.prototype.concat = function () {
var others = [];
for (var _i = 0; _i < arguments.length; _i++) {
others[_i] = arguments[_i];
}
if (others.every(isEmptyAsyncStreamSourceInstance))
return this;
return new AsyncConcatStream(this, others);
};
AsyncStreamBase.prototype.min = function (otherwise) {
return this.minBy(common_1.Comp.defaultComp().compare, otherwise);
};
AsyncStreamBase.prototype.minBy = function (compare, otherwise) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var done, iterator, result, value, _b, err_4;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
_c.label = 1;
case 1:
_c.trys.push([1, 6, , 8]);
return [4 /*yield*/, iterator.fastNext(done)];
case 2:
result = _c.sent();
if (done === result)
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
_c.label = 3;
case 3:
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 4:
if (!(_b !== (value = _c.sent()))) return [3 /*break*/, 5];
if (compare(value, result) < 0)
result = value;
return [3 /*break*/, 3];
case 5: return [2 /*return*/, result];
case 6:
err_4 = _c.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_c.sent();
throw err_4;
case 8: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.max = function (otherwise) {
return this.maxBy(common_1.Comp.defaultComp().compare, otherwise);
};
AsyncStreamBase.prototype.maxBy = function (compare, otherwise) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var done, iterator, result, value, _b, err_5;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
_c.label = 1;
case 1:
_c.trys.push([1, 6, , 8]);
return [4 /*yield*/, iterator.fastNext(done)];
case 2:
result = _c.sent();
if (done === result) {
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(otherwise)];
}
value = void 0;
_c.label = 3;
case 3:
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 4:
if (!(_b !== (value = _c.sent()))) return [3 /*break*/, 5];
if (compare(value, result) > 0)
result = value;
return [3 /*break*/, 3];
case 5: return [2 /*return*/, result];
case 6:
err_5 = _c.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_c.sent();
throw err_5;
case 8: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.intersperse = function (sep) {
if (isEmptyAsyncStreamSourceInstance(sep)) {
return this;
}
return this.transform(async_1.AsyncTransformer.intersperse(sep));
};
AsyncStreamBase.prototype.join = function () {
return tslib_1.__awaiter(this, arguments, void 0, function (_b) {
var done, iterator, value, result, _c, err_6;
var _d = _b === void 0 ? {} : _b, _e = _d.sep, sep = _e === void 0 ? '' : _e, _f = _d.start, start = _f === void 0 ? '' : _f, _g = _d.end, end = _g === void 0 ? '' : _g, _h = _d.valueToString, valueToString = _h === void 0 ? String : _h, _j = _d.ifEmpty, ifEmpty = _j === void 0 ? undefined : _j;
return tslib_1.__generator(this, function (_k) {
switch (_k.label) {
case 0:
done = Symbol('Done');
iterator = this[Symbol.asyncIterator]();
return [4 /*yield*/, iterator.fastNext(done)];
case 1:
value = _k.sent();
_k.label = 2;
case 2:
_k.trys.push([2, 6, , 8]);
if (done === value) {
if (undefined !== ifEmpty)
return [2 /*return*/, ifEmpty];
return [2 /*return*/, start.concat(end)];
}
result = start.concat(valueToString(value));
_k.label = 3;
case 3:
_c = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 4:
if (!(_c !== (value = _k.sent()))) return [3 /*break*/, 5];
result = result.concat(sep, valueToString(value));
return [3 /*break*/, 3];
case 5: return [2 /*return*/, result.concat(end)];
case 6:
err_6 = _k.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 7:
_k.sent();
throw err_6;
case 8: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.mkGroup = function (_b) {
var _c = _b === void 0 ? {} : _b, _d = _c.sep, sep = _d === void 0 ? exports.emptyAsyncStream : _d, _e = _c.start, start = _e === void 0 ? exports.emptyAsyncStream : _e, _f = _c.end, end = _f === void 0 ? exports.emptyAsyncStream : _f;
return (0, exports.fromAsyncStreamSource)(start).concat(this.intersperse(sep), end);
};
AsyncStreamBase.prototype.splitWhere = function (pred, options) {
if (options === void 0) { options = {}; }
return this.transform(async_1.AsyncTransformer.splitWhere(pred, options));
};
AsyncStreamBase.prototype.splitOn = function (sepElem, options) {
if (options === void 0) { options = {}; }
return this.transform(async_1.AsyncTransformer.splitOn(sepElem, options));
};
AsyncStreamBase.prototype.splitOnSlice = function (sepSlice, options) {
if (options === void 0) { options = {}; }
return this.transform(async_1.AsyncTransformer.splitOnSlice(sepSlice, options));
};
AsyncStreamBase.prototype.distinctPrevious = function (options) {
if (options === void 0) { options = {}; }
return this.transform(stream_1.Transformer.distinctPrevious(options));
};
AsyncStreamBase.prototype.window = function (windowSize, options) {
if (options === void 0) { options = {}; }
return this.transform(async_1.AsyncTransformer.window(windowSize, options));
};
AsyncStreamBase.prototype.partition = function (pred, options) {
if (options === void 0) { options = {}; }
return this.reduce(async_1.AsyncReducer.partition(pred, options));
};
AsyncStreamBase.prototype.groupBy = function (valueToKey, options) {
if (options === void 0) { options = {}; }
return this.reduce(async_1.AsyncReducer.groupBy(valueToKey, options));
};
AsyncStreamBase.prototype.fold = function (init, next) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_b) {
return [2 /*return*/, this.reduce(async_1.AsyncReducer.fold(init, next))];
});
});
};
AsyncStreamBase.prototype.foldStream = function (init, next) {
return this.reduceStream(async_1.AsyncReducer.fold(init, next));
};
AsyncStreamBase.prototype.reduce = function (shape) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var reducerInstance, done, value, iter, _b, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0: return [4 /*yield*/, async_1.AsyncReducer.combine(shape).compile()];
case 1:
reducerInstance = (_d.sent());
done = Symbol('Done');
value = done;
iter = this[Symbol.asyncIterator]();
_d.label = 2;
case 2:
_d.trys.push([2, , 9, 12]);
_d.label = 3;
case 3:
_b = !reducerInstance.halted;
if (!_b) return [3 /*break*/, 5];
_c = done;
return [4 /*yield*/, iter.fastNext(done)];
case 4:
_b = _c !== (value = _d.sent());
_d.label = 5;
case 5:
if (!_b) return [3 /*break*/, 7];
return [4 /*yield*/, reducerInstance.next(value)];
case 6:
_d.sent();
return [3 /*break*/, 3];
case 7: return [4 /*yield*/, reducerInstance.getOutput()];
case 8: return [2 /*return*/, _d.sent()];
case 9: return [4 /*yield*/, (0, async_custom_1.closeIters)(iter)];
case 10:
_d.sent();
return [4 /*yield*/, reducerInstance.onClose()];
case 11:
_d.sent();
return [7 /*endfinally*/];
case 12: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.reduceStream = function (shape) {
var reducer = async_1.AsyncReducer.combine(shape);
return new AsyncReduceStream(this, reducer);
};
AsyncStreamBase.prototype.toArray = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var iterator, result, done, value, _b, err_7;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
iterator = this[Symbol.asyncIterator]();
result = [];
done = Symbol('Done');
_c.label = 1;
case 1:
_c.trys.push([1, 5, , 7]);
_c.label = 2;
case 2:
_b = done;
return [4 /*yield*/, iterator.fastNext(done)];
case 3:
if (!(_b !== (value = _c.sent()))) return [3 /*break*/, 4];
result.push(value);
return [3 /*break*/, 2];
case 4: return [2 /*return*/, result];
case 5:
err_7 = _c.sent();
return [4 /*yield*/, (0, async_custom_1.closeIters)(iterator)];
case 6:
_c.sent();
throw err_7;
case 7: return [2 /*return*/];
}
});
});
};
AsyncStreamBase.prototype.toString = function () {
return "AsyncStream(...<potentially empty>)";
};
AsyncStreamBase.prototype.toJSON = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = {
dataType: 'AsyncStream'
};
return [4 /*yield*/, this.toArray()];
case 1: return [2 /*return*/, (_b.value = _c.sent(),
_b)];
}
});
});
};
return AsyncStreamBase;
}());
exports.AsyncStreamBase = AsyncStreamBase;
var AsyncFromStream = /** @class */ (function (_super) {
tslib_1.__extends(AsyncFromStream, _super);
function AsyncFromStream(createIterator) {
var _this = _super.call(this) || this;
_this[_a] = undefined;
_this[Symbol.asyncIterator] = createIterator;
return _this;
}
return AsyncFromStream;
}(AsyncStreamBase));
exports.AsyncFromStream = AsyncFromStream;
_a = Symbol.asyncIterator;
var AsyncPrependStream = /** @class */ (function (_super) {
tslib_1.__extends(AsyncPrependStream, _super);
function AsyncPrependStream(source, item) {
var _this = _super.call(this) || this;
_this.source = source;
_this.item = item;
return _this;
}
AsyncPrependStream.prototype[Symbol.asyncIterator] = function () {
return new async_custom_1.AsyncPrependIterator(this.source[Symbol.asyncIterator](), this.item);
};
AsyncPrependStream.prototype.first = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_b) {
return [2 /*return*/, common_1.AsyncOptLazy.toPromise(this.item)];
});
});
};
AsyncPrependStream.prototype.last = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_b) {
return [2 /*return*/, this.source.last(this.item)];
});
});
};
AsyncPrependStream.prototype.count = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.source.count()];
case 1: return [2 /*return*/, (_b.sent()) + 1];
}
});
});
};
AsyncPrependStream.prototype.forEach = function (f_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (f, options) {
var _b, state, _c;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_b = options.state, state = _b === void 0 ? (0, common_1.TraverseState)() : _b;
if (state.halted)
return [2 /*return*/];
_c = f;
return [4 /*yield*/, common_1.AsyncOptLazy.toMaybePromise(this.item)];
case 1: return [4 /*yield*/, _c.apply(void 0, [_d.sent(), state.nextIndex(),
state.halt])];
case 2:
_d.sent();
if (state.halted)
return [2 /*return*/];
return [4 /*yield*/, this.source.forEach(f, { state: state })];
case 3:
_d.sent();
return [2 /*return*/];
}
});
});
};
AsyncPrependStream.prototype.mapPure = function (mapFun) {
var _b;
var _this = this;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return new AsyncPrependStream((_b = this.source).mapPure.apply(_b, tslib_1.__spreadArray([mapFun], tslib_1.__read(args), false)), function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _b, _c, _d; return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_c = (_b = mapFun).apply;
_d = [void 0];
return [4 /*yield*/, common_1.AsyncOptLazy.toMaybePromise(this.item)];
case 1: return [2 /*return*/, _c.apply(_b, _d.concat([tslib_1.__spreadArray.apply(void 0, [[_e.sent()], tslib_1.__read(args), false])]))];
}
}); }); });
};
AsyncPrependStream.prototype.take = function (amount) {
if (amount <= 0) {
return exports.emptyAsyncStream;
}
if (amount === 1) {
return exports.AsyncStreamConstructorsImpl.of(this.item);
}
return new AsyncPrependStream(this.source.take(amount - 1), this.item);
};
AsyncPrependStream.prototype.drop = function (amount) {
if (amount <= 0) {
return this;
}
if (amount === 1) {
return this.source;
}
return this.source.drop(amount - 1);
};
AsyncPrependStream.prototype.minBy = function (compare) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var token, result, itemValue;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
token = Symbol();
return [4 /*yield*/, this.source.minBy(compare, token)];
case 1:
result = _b.sent();
return [4 /*yield*/, common_1.AsyncOptLazy.toMaybePromise(this.item)];
case 2:
itemValue = _b.sent();
if (token === result) {
return [2 /*return*/, itemValue];
}
return [2 /*return*/, compare(result, itemValue) <= 0 ? result : itemValue];
}
});
});
};
AsyncPrependStream.prototype.maxBy = function (compare) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var token, result, itemValue;
return tslib_1.__generator(t