UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

78 lines 2.9 kB
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 __()); }; define(["require", "exports", '../Subscriber', '../util/ArgumentOutOfRangeError', '../observable/EmptyObservable'], function (require, exports, Subscriber_1, ArgumentOutOfRangeError_1, EmptyObservable_1) { "use strict"; /** * @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 */ function takeLast(total) { if (total === 0) { return new EmptyObservable_1.EmptyObservable(); } else { return this.lift(new TakeLastOperator(total)); } } 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