@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
109 lines • 3.84 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Subscriber_1 = require("../Subscriber");
var ArgumentOutOfRangeError_1 = require("../util/ArgumentOutOfRangeError");
/**
* Skip the last `count` values emitted by the source Observable.
*
* 
*
* `skipLast` returns an Observable that accumulates a queue with a length
* enough to store the first `count` values. As more values are received,
* values are taken from the front of the queue and produced on the result
* sequence. This causes values to be delayed.
*
* ## Example
* Skip the last 2 values of an Observable with many values
* ```ts
* import { range } from 'rxjs';
* import { skipLast } from 'rxjs/operators';
*
* const many = range(1, 5);
* const skipLastTwo = many.pipe(skipLast(2));
* skipLastTwo.subscribe(x => console.log(x));
*
* // Results in:
* // 1 2 3
* ```
*
* @see {@link skip}
* @see {@link skipUntil}
* @see {@link skipWhile}
* @see {@link take}
*
* @throws {ArgumentOutOfRangeError} When using `skipLast(i)`, it throws
* ArgumentOutOrRangeError if `i < 0`.
*
* @param {number} count Number of elements to skip from the end of the source Observable.
* @returns {Observable<T>} An Observable that skips the last count values
* emitted by the source Observable.
* @method skipLast
* @owner Observable
*/
function skipLast(count) {
return function (source) { return source.lift(new SkipLastOperator(count)); };
}
exports.skipLast = skipLast;
var SkipLastOperator = /** @class */ (function () {
function SkipLastOperator(_skipCount) {
this._skipCount = _skipCount;
if (this._skipCount < 0) {
throw new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError;
}
}
SkipLastOperator.prototype.call = function (subscriber, source) {
if (this._skipCount === 0) {
// If we don't want to skip any values then just subscribe
// to Subscriber without any further logic.
return source.subscribe(new Subscriber_1.Subscriber(subscriber));
}
else {
return source.subscribe(new SkipLastSubscriber(subscriber, this._skipCount));
}
};
return SkipLastOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var SkipLastSubscriber = /** @class */ (function (_super) {
__extends(SkipLastSubscriber, _super);
function SkipLastSubscriber(destination, _skipCount) {
var _this = _super.call(this, destination) || this;
_this._skipCount = _skipCount;
_this._count = 0;
_this._ring = new Array(_skipCount);
return _this;
}
SkipLastSubscriber.prototype._next = function (value) {
var skipCount = this._skipCount;
var count = this._count++;
if (count < skipCount) {
this._ring[count] = value;
}
else {
var currentIndex = count % skipCount;
var ring = this._ring;
var oldValue = ring[currentIndex];
ring[currentIndex] = value;
this.destination.next(oldValue);
}
};
return SkipLastSubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=skipLast.js.map