es6/operator/skipLast.js
import { Subscriber } from '../Subscriber';
import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
/**
* 跳过源 Observable 最后发出的的N个值 (N = count)。
*
* <img src="./img/skipLast.png" width="100%">
*
* `skipLast` 返回一个 Observable,该 Observable 累积足够长的队列以存储最初的N个值 (N = count)。
* 当接收到更多值时,将从队列的前面取值并在结果序列上产生。 这种情况下值会被延时。
*
* @example <caption>跳过有多个值的 Observable 的最后2个值</caption>
* var many = Rx.Observable.range(1, 5);
* var skipLastTwo = many.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} 当使用 `skipLast(i)` 时, 如果`i < 0`,则
* 抛出 ArgumentOutOrRangeError。
*
* @param {number} count 源 Observable 中从后往前要跳过的值的数量。
* @returns {Observable<T>} Observable 跳过源 Observable 发出
* 的最后几个值。
* @method skipLast
* @owner Observable
*/
export function skipLast(count) {
return this.lift(new SkipLastOperator(count));
}
class SkipLastOperator {
constructor(_skipCount) {
this._skipCount = _skipCount;
if (this._skipCount < 0) {
throw new ArgumentOutOfRangeError;
}
}
call(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(subscriber));
}
else {
return source.subscribe(new SkipLastSubscriber(subscriber, this._skipCount));
}
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SkipLastSubscriber extends Subscriber {
constructor(destination, _skipCount) {
super(destination);
this._skipCount = _skipCount;
this._count = 0;
this._ring = new Array(_skipCount);
}
_next(value) {
const skipCount = this._skipCount;
const count = this._count++;
if (count < skipCount) {
this._ring[count] = value;
}
else {
const currentIndex = count % skipCount;
const ring = this._ring;
const oldValue = ring[currentIndex];
ring[currentIndex] = value;
this.destination.next(oldValue);
}
}
}
//# sourceMappingURL=skipLast.js.map