@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
66 lines (64 loc) • 2.81 kB
JavaScript
import { __asyncValues, __awaiter, __generator } from "tslib";
import { wrapWithAbort } from './operators/withabort.js';
import { throwIfAborted } from '../aborterror.js';
/**
* Returns the element at a specified index in a sequence or undefined if the index is out of range.
*
* @template T The type of the elements in the source sequence.
* @param {AsyncIterable<T>} source async-iterable sequence to return the element from.
* @param {number} index The zero-based index of the element to retrieve.
* @param {AbortSignal} [signal] The optional abort signal to be used for cancelling the sequence at any time.
* @returns {(Promise<T | undefined>)} An async-iterable sequence that produces the element at the specified
* position in the source sequence, or undefined if the index is outside the bounds of the source sequence.
*/
export function elementAt(source, index, signal) {
var _a, e_1, _b, _c;
return __awaiter(this, void 0, void 0, function () {
var i, _d, _e, _f, item, e_1_1;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
throwIfAborted(signal);
i = index;
_g.label = 1;
case 1:
_g.trys.push([1, 6, 7, 12]);
_d = true, _e = __asyncValues(wrapWithAbort(source, signal));
_g.label = 2;
case 2: return [4 /*yield*/, _e.next()];
case 3:
if (!(_f = _g.sent(), _a = _f.done, !_a)) return [3 /*break*/, 5];
_c = _f.value;
_d = false;
item = _c;
if (i === 0) {
return [2 /*return*/, item];
}
i--;
_g.label = 4;
case 4:
_d = true;
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 12];
case 6:
e_1_1 = _g.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 12];
case 7:
_g.trys.push([7, , 10, 11]);
if (!(!_d && !_a && (_b = _e.return))) return [3 /*break*/, 9];
return [4 /*yield*/, _b.call(_e)];
case 8:
_g.sent();
_g.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12: return [2 /*return*/, undefined];
}
});
});
}
//# sourceMappingURL=elementat.js.map