@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
69 lines • 1.95 kB
JavaScript
import { Subscriber } from '../Subscriber';
import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
import { EmptyObservable } from '../observable/EmptyObservable';
/**
* @throws {ArgumentOutOfRangeError} When using `takeLast(i)`, it delivers an
* ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
* @param total
* @return {any}
* @method takeLast
* @owner Observable
*/
export function takeLast(total) {
if (total === 0) {
return new EmptyObservable();
}
else {
return this.lift(new TakeLastOperator(total));
}
}
class TakeLastOperator {
constructor(total) {
this.total = total;
if (this.total < 0) {
throw new ArgumentOutOfRangeError;
}
}
call(subscriber, source) {
return source._subscribe(new TakeLastSubscriber(subscriber, this.total));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class TakeLastSubscriber extends Subscriber {
constructor(destination, total) {
super(destination);
this.total = total;
this.ring = new Array();
this.count = 0;
}
_next(value) {
const ring = this.ring;
const total = this.total;
const count = this.count++;
if (ring.length < total) {
ring.push(value);
}
else {
const index = count % total;
ring[index] = value;
}
}
_complete() {
const destination = this.destination;
let count = this.count;
if (count > 0) {
const total = this.count >= this.total ? this.total : this.count;
const ring = this.ring;
for (let i = 0; i < total; i++) {
const idx = (count++) % total;
destination.next(ring[idx]);
}
}
destination.complete();
}
}
//# sourceMappingURL=takeLast.js.map