"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Subscriber_1 = require('../Subscriber');
var ArgumentOutOfRangeError_1 = require('../util/ArgumentOutOfRangeError');
var EmptyObservable_1 = require('../observable/EmptyObservable');
/**
* Emits only the last `count` values emitted by the source Observable.
*
* Remembers the latest `count` values, then emits those
* only when the source completes.
*
*
*
* `takeLast` returns an Observable that emits at most the last `count` values
* emitted by the source Observable. If the source emits fewer than `count`
* values then all of its values are emitted. This operator must wait until the
* `complete` notification emission from the source in order to emit the `next`
* values on the output Observable, because otherwise it is impossible to know
* whether or not more values will be emitted on the source. For this reason,
* all values are emitted synchronously, followed by the complete notification.
*
* @example
Take the last 3 values of an Observable with many values
* var many = Rx.Observable.range(1, 100);
* var lastThree = many.takeLast(3);
* lastThree.subscribe(x => console.log(x));
*
* @see {@link take}
* @see {@link takeUntil}
* @see {@link takeWhile}
* @see {@link skip}
*
* @throws {ArgumentOutOfRangeError} When using `takeLast(i)`, it delivers an
* ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
*
* @param {number} count The maximum number of values to emit from the end of
* the sequence of values emitted by the source Observable.
* @return {Observable} An Observable that emits at most the last count
* values emitted by the source Observable.
* @method takeLast
* @owner Observable
*/
function takeLast(count) {
return function takeLastOperatorFunction(source) {
if (count === 0) {
return new EmptyObservable_1.EmptyObservable();
}
else {
return source.lift(new TakeLastOperator(count));
}
};
}
exports.takeLast = takeLast;
var TakeLastOperator = (function () {
function TakeLastOperator(total) {
this.total = total;
if (this.total < 0) {
throw new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError;
}
}
TakeLastOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
};
return TakeLastOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var TakeLastSubscriber = (function (_super) {
__extends(TakeLastSubscriber, _super);
function TakeLastSubscriber(destination, total) {
_super.call(this, destination);
this.total = total;
this.ring = new Array();
this.count = 0;
}
TakeLastSubscriber.prototype._next = function (value) {
var ring = this.ring;
var total = this.total;
var count = this.count++;
if (ring.length < total) {
ring.push(value);
}
else {
var index = count % total;
ring[index] = value;
}
};
TakeLastSubscriber.prototype._complete = function () {
var destination = this.destination;
var count = this.count;
if (count > 0) {
var total = this.count >= this.total ? this.total : this.count;
var ring = this.ring;
for (var i = 0; i < total; i++) {
var idx = (count++) % total;
destination.next(ring[idx]);
}
}
destination.complete();
};
return TakeLastSubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=takeLast.js.map