Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.6 KB
|
Line | |
---|
1 | import { Subscriber } from '../Subscriber';
|
---|
2 | import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
|
---|
3 | import { empty } from '../observable/empty';
|
---|
4 | export function takeLast(count) {
|
---|
5 | return function takeLastOperatorFunction(source) {
|
---|
6 | if (count === 0) {
|
---|
7 | return empty();
|
---|
8 | }
|
---|
9 | else {
|
---|
10 | return source.lift(new TakeLastOperator(count));
|
---|
11 | }
|
---|
12 | };
|
---|
13 | }
|
---|
14 | class TakeLastOperator {
|
---|
15 | constructor(total) {
|
---|
16 | this.total = total;
|
---|
17 | if (this.total < 0) {
|
---|
18 | throw new ArgumentOutOfRangeError;
|
---|
19 | }
|
---|
20 | }
|
---|
21 | call(subscriber, source) {
|
---|
22 | return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
|
---|
23 | }
|
---|
24 | }
|
---|
25 | class TakeLastSubscriber extends Subscriber {
|
---|
26 | constructor(destination, total) {
|
---|
27 | super(destination);
|
---|
28 | this.total = total;
|
---|
29 | this.ring = new Array();
|
---|
30 | this.count = 0;
|
---|
31 | }
|
---|
32 | _next(value) {
|
---|
33 | const ring = this.ring;
|
---|
34 | const total = this.total;
|
---|
35 | const count = this.count++;
|
---|
36 | if (ring.length < total) {
|
---|
37 | ring.push(value);
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | const index = count % total;
|
---|
41 | ring[index] = value;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | _complete() {
|
---|
45 | const destination = this.destination;
|
---|
46 | let count = this.count;
|
---|
47 | if (count > 0) {
|
---|
48 | const total = this.count >= this.total ? this.total : this.count;
|
---|
49 | const ring = this.ring;
|
---|
50 | for (let i = 0; i < total; i++) {
|
---|
51 | const idx = (count++) % total;
|
---|
52 | destination.next(ring[idx]);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | destination.complete();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | //# sourceMappingURL=takeLast.js.map |
---|
Note:
See
TracBrowser
for help on using the repository browser.