1 | import { AsyncIterableX } from './asynciterablex';
|
2 | export class TakeLastAsyncIterable extends AsyncIterableX {
|
3 | constructor(source, count) {
|
4 | super();
|
5 | this._source = source;
|
6 | this._count = count;
|
7 | }
|
8 | async *[Symbol.asyncIterator]() {
|
9 | if (this._count > 0) {
|
10 | let q = [];
|
11 | for await (let item of this._source) {
|
12 | if (q.length >= this._count) {
|
13 | q.shift();
|
14 | }
|
15 | q.push(item);
|
16 | }
|
17 | while (q.length > 0) {
|
18 | yield q.shift();
|
19 | }
|
20 | }
|
21 | }
|
22 | }
|
23 | export function takeLast(source, count) {
|
24 | return new TakeLastAsyncIterable(source, count);
|
25 | }
|
26 |
|
27 |
|