mirror of
https://github.com/biobulkbende/biobulkbende.org.git
synced 2024-12-27 05:15:25 +00:00
81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
import { Subscriber } from '../Subscriber';
|
|
import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
|
|
import { EmptyObservable } from '../observable/EmptyObservable';
|
|
/**
|
|
* Emits only the first `count` values emitted by the source Observable.
|
|
*
|
|
* <span class="informal">Takes the first `count` values from the source, then
|
|
* completes.</span>
|
|
*
|
|
* <img src="./img/take.png" width="100%">
|
|
*
|
|
* `take` returns an Observable that emits only the first `count` values emitted
|
|
* by the source Observable. If the source emits fewer than `count` values then
|
|
* all of its values are emitted. After that, it completes, regardless if the
|
|
* source completes.
|
|
*
|
|
* @example <caption>Take the first 5 seconds of an infinite 1-second interval Observable</caption>
|
|
* var interval = Rx.Observable.interval(1000);
|
|
* var five = interval.take(5);
|
|
* five.subscribe(x => console.log(x));
|
|
*
|
|
* @see {@link takeLast}
|
|
* @see {@link takeUntil}
|
|
* @see {@link takeWhile}
|
|
* @see {@link skip}
|
|
*
|
|
* @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an
|
|
* ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
|
|
*
|
|
* @param {number} count The maximum number of `next` values to emit.
|
|
* @return {Observable<T>} An Observable that emits only the first `count`
|
|
* values emitted by the source Observable, or all of the values from the source
|
|
* if the source emits fewer than `count` values.
|
|
* @method take
|
|
* @owner Observable
|
|
*/
|
|
export function take(count) {
|
|
return (source) => {
|
|
if (count === 0) {
|
|
return new EmptyObservable();
|
|
}
|
|
else {
|
|
return source.lift(new TakeOperator(count));
|
|
}
|
|
};
|
|
}
|
|
class TakeOperator {
|
|
constructor(total) {
|
|
this.total = total;
|
|
if (this.total < 0) {
|
|
throw new ArgumentOutOfRangeError;
|
|
}
|
|
}
|
|
call(subscriber, source) {
|
|
return source.subscribe(new TakeSubscriber(subscriber, this.total));
|
|
}
|
|
}
|
|
/**
|
|
* We need this JSDoc comment for affecting ESDoc.
|
|
* @ignore
|
|
* @extends {Ignored}
|
|
*/
|
|
class TakeSubscriber extends Subscriber {
|
|
constructor(destination, total) {
|
|
super(destination);
|
|
this.total = total;
|
|
this.count = 0;
|
|
}
|
|
_next(value) {
|
|
const total = this.total;
|
|
const count = ++this.count;
|
|
if (count <= total) {
|
|
this.destination.next(value);
|
|
if (count === total) {
|
|
this.destination.complete();
|
|
this.unsubscribe();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//# sourceMappingURL=take.js.map
|