Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / takeLast.ts
1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
4 import { empty } from '../observable/empty';
5 import { Observable } from '../Observable';
6 import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
7
8 /**
9  * Emits only the last `count` values emitted by the source Observable.
10  *
11  * <span class="informal">Remembers the latest `count` values, then emits those
12  * only when the source completes.</span>
13  *
14  * ![](takeLast.png)
15  *
16  * `takeLast` returns an Observable that emits at most the last `count` values
17  * emitted by the source Observable. If the source emits fewer than `count`
18  * values then all of its values are emitted. This operator must wait until the
19  * `complete` notification emission from the source in order to emit the `next`
20  * values on the output Observable, because otherwise it is impossible to know
21  * whether or not more values will be emitted on the source. For this reason,
22  * all values are emitted synchronously, followed by the complete notification.
23  *
24  * ## Example
25  * Take the last 3 values of an Observable with many values
26  * ```ts
27  * import { range } from 'rxjs';
28  * import { takeLast } from 'rxjs/operators';
29  *
30  * const many = range(1, 100);
31  * const lastThree = many.pipe(takeLast(3));
32  * lastThree.subscribe(x => console.log(x));
33  * ```
34  *
35  * @see {@link take}
36  * @see {@link takeUntil}
37  * @see {@link takeWhile}
38  * @see {@link skip}
39  *
40  * @throws {ArgumentOutOfRangeError} When using `takeLast(i)`, it delivers an
41  * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
42  *
43  * @param {number} count The maximum number of values to emit from the end of
44  * the sequence of values emitted by the source Observable.
45  * @return {Observable<T>} An Observable that emits at most the last count
46  * values emitted by the source Observable.
47  * @method takeLast
48  * @owner Observable
49  */
50 export function takeLast<T>(count: number): MonoTypeOperatorFunction<T> {
51   return function takeLastOperatorFunction(source: Observable<T>): Observable<T> {
52     if (count === 0) {
53       return empty();
54     } else {
55       return source.lift(new TakeLastOperator(count));
56     }
57   };
58 }
59
60 class TakeLastOperator<T> implements Operator<T, T> {
61   constructor(private total: number) {
62     if (this.total < 0) {
63       throw new ArgumentOutOfRangeError;
64     }
65   }
66
67   call(subscriber: Subscriber<T>, source: any): TeardownLogic {
68     return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
69   }
70 }
71
72 /**
73  * We need this JSDoc comment for affecting ESDoc.
74  * @ignore
75  * @extends {Ignored}
76  */
77 class TakeLastSubscriber<T> extends Subscriber<T> {
78   private ring: Array<T> = new Array();
79   private count: number = 0;
80
81   constructor(destination: Subscriber<T>, private total: number) {
82     super(destination);
83   }
84
85   protected _next(value: T): void {
86     const ring = this.ring;
87     const total = this.total;
88     const count = this.count++;
89
90     if (ring.length < total) {
91       ring.push(value);
92     } else {
93       const index = count % total;
94       ring[index] = value;
95     }
96   }
97
98   protected _complete(): void {
99     const destination = this.destination;
100     let count = this.count;
101
102     if (count > 0) {
103       const total = this.count >= this.total ? this.total : this.count;
104       const ring  = this.ring;
105
106       for (let i = 0; i < total; i++) {
107         const idx = (count++) % total;
108         destination.next(ring[idx]);
109       }
110     }
111
112     destination.complete();
113   }
114 }