ix
Version:
The Interactive Extensions for JavaScript
58 lines (56 loc) • 2.22 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await } from "tslib";
import { AsyncIterableX } from '../asynciterablex.mjs';
import { wrapWithAbort } from './withabort.mjs';
import { throwIfAborted } from '../../aborterror.mjs';
/** @ignore */
export class TakeLastAsyncIterable extends AsyncIterableX {
constructor(source, count) {
super();
this._source = source;
this._count = count;
}
[Symbol.asyncIterator](signal) {
return __asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
throwIfAborted(signal);
if (this._count > 0) {
const q = [];
try {
for (var _e = true, _f = __asyncValues(wrapWithAbort(this._source, signal)), _g; _g = yield __await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
if (q.length >= this._count) {
q.shift();
}
q.push(item);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield __await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
while (q.length > 0) {
yield yield __await(q.shift());
}
}
});
}
}
/**
* Returns a specified number of contiguous elements from the end of an async-iterable sequence.
*
* @template TSource The type of the elements in the source sequence.
* @param {number} count Number of elements to take from the end of the source sequence.
* @returns {MonoTypeOperatorAsyncFunction<TSource>} An async-iterable sequence containing the specified
* number of elements from the end of the source sequence.
*/
export function takeLast(count) {
return function takeLastOperatorFunction(source) {
return new TakeLastAsyncIterable(source, count);
};
}
//# sourceMappingURL=takelast.mjs.map