UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

71 lines (69 loc) 3.26 kB
import { __asyncValues, __awaiter, __generator } from "tslib"; import { wrapWithAbort } from './operators/withabort.js'; import { throwIfAborted } from '../aborterror.js'; /** * Returns the last element of an async-iterable sequence that satisfies the condition in the predicate if given * otherwise the last item in the sequence, or a default value if no such element exists. * * @template T The type of elements in the source sequence. * @param {AsyncIterable<T>} source The source async-iterable sequence. * @param {OptionalFindOptions<T, S>} [options] The options which include an optional predicate for filtering, * thirArg for binding, and abort signal for cancellation * @returns {(Promise<S | undefined>)} A promise containing the last value that matches the optional predicate or last item, otherwise undefined. */ export function last(source, options) { var _a, e_1, _b, _c; return __awaiter(this, void 0, void 0, function () { var _d, signal, thisArg, _e, predicate, i, result, _f, _g, _h, item, e_1_1; var _this = this; return __generator(this, function (_j) { switch (_j.label) { case 0: _d = options || {}, signal = _d["signal"], thisArg = _d["thisArg"], _e = _d["predicate"], predicate = _e === void 0 ? function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, true]; }); }); } : _e; throwIfAborted(signal); i = 0; _j.label = 1; case 1: _j.trys.push([1, 7, 8, 13]); _f = true, _g = __asyncValues(wrapWithAbort(source, signal)); _j.label = 2; case 2: return [4 /*yield*/, _g.next()]; case 3: if (!(_h = _j.sent(), _a = _h.done, !_a)) return [3 /*break*/, 6]; _c = _h.value; _f = false; item = _c; return [4 /*yield*/, predicate.call(thisArg, item, i++, signal)]; case 4: if (_j.sent()) { result = item; } _j.label = 5; case 5: _f = true; return [3 /*break*/, 2]; case 6: return [3 /*break*/, 13]; case 7: e_1_1 = _j.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 13]; case 8: _j.trys.push([8, , 11, 12]); if (!(!_f && !_a && (_b = _g.return))) return [3 /*break*/, 10]; return [4 /*yield*/, _b.call(_g)]; case 9: _j.sent(); _j.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*/, result]; } }); }); } //# sourceMappingURL=last.js.map