work.suroh.tk/node_modules/rxjs/_esm5/operators/takeLast.js

111 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-12-02 12:22:45 +00:00
/** PURE_IMPORTS_START .._Subscriber,.._util_ArgumentOutOfRangeError,.._observable_EmptyObservable PURE_IMPORTS_END */
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 __());
};
import { Subscriber } from '../Subscriber';
import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
import { EmptyObservable } from '../observable/EmptyObservable';
/**
* Emits only the last `count` values emitted by the source Observable.
*
* <span class="informal">Remembers the latest `count` values, then emits those
* only when the source completes.</span>
*
* <img src="./img/takeLast.png" width="100%">
*
* `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 <caption>Take the last 3 values of an Observable with many values</caption>
* 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<T>} An Observable that emits at most the last count
* values emitted by the source Observable.
* @method takeLast
* @owner Observable
*/
export function takeLast(count) {
return function takeLastOperatorFunction(source) {
if (count === 0) {
return new EmptyObservable();
}
else {
return source.lift(new TakeLastOperator(count));
}
};
}
var TakeLastOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
function TakeLastOperator(total) {
this.total = total;
if (this.total < 0) {
throw new 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 = /*@__PURE__*/ (/*@__PURE__*/ 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));
//# sourceMappingURL=takeLast.js.map