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';
9 * Emits only the last `count` values emitted by the source Observable.
11 * <span class="informal">Remembers the latest `count` values, then emits those
12 * only when the source completes.</span>
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.
25 * Take the last 3 values of an Observable with many values
27 * import { range } from 'rxjs';
28 * import { takeLast } from 'rxjs/operators';
30 * const many = range(1, 100);
31 * const lastThree = many.pipe(takeLast(3));
32 * lastThree.subscribe(x => console.log(x));
36 * @see {@link takeUntil}
37 * @see {@link takeWhile}
40 * @throws {ArgumentOutOfRangeError} When using `takeLast(i)`, it delivers an
41 * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
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.
50 export function takeLast<T>(count: number): MonoTypeOperatorFunction<T> {
51 return function takeLastOperatorFunction(source: Observable<T>): Observable<T> {
55 return source.lift(new TakeLastOperator(count));
60 class TakeLastOperator<T> implements Operator<T, T> {
61 constructor(private total: number) {
63 throw new ArgumentOutOfRangeError;
67 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
68 return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
73 * We need this JSDoc comment for affecting ESDoc.
77 class TakeLastSubscriber<T> extends Subscriber<T> {
78 private ring: Array<T> = new Array();
79 private count: number = 0;
81 constructor(destination: Subscriber<T>, private total: number) {
85 protected _next(value: T): void {
86 const ring = this.ring;
87 const total = this.total;
88 const count = this.count++;
90 if (ring.length < total) {
93 const index = count % total;
98 protected _complete(): void {
99 const destination = this.destination;
100 let count = this.count;
103 const total = this.count >= this.total ? this.total : this.count;
104 const ring = this.ring;
106 for (let i = 0; i < total; i++) {
107 const idx = (count++) % total;
108 destination.next(ring[idx]);
112 destination.complete();