@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
53 lines • 1.44 kB
JavaScript
import { Subscriber } from '../Subscriber';
import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
import { EmptyObservable } from '../observable/EmptyObservable';
/**
* @throws {ArgumentOutOfRangeError} When using `take(i)`, it delivers an
* ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
* @param total
* @return {any}
* @method take
* @owner Observable
*/
export function take(total) {
if (total === 0) {
return new EmptyObservable();
}
else {
return this.lift(new TakeOperator(total));
}
}
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;
if (++this.count <= total) {
this.destination.next(value);
if (this.count === total) {
this.destination.complete();
this.unsubscribe();
}
}
}
}
//# sourceMappingURL=take.js.map