Code coverage report for src/operator/take.ts

Statements: 100% (29 / 29)      Branches: 87.5% (7 / 8)      Functions: 100% (7 / 7)      Lines: 100% (25 / 25)      Ignored: none     

All files » src/operator/ » take.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  1 1 1   1 27 1   26       1 26 26 1       1 25   1   1 25   25 25     1 54 54 54 54 16       1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {ArgumentOutOfRangeError} from '../util/ArgumentOutOfRangeError';
import {EmptyObservable} from '../observable/empty';
 
export function take(total) {
  if (total === 0) {
    return new EmptyObservable();
  } else {
    return this.lift(new TakeOperator(total));
  }
}
 
class TakeOperator<T, R> implements Operator<T, R> {
  constructor(private total: number) {
    if (this.total < 0) {
      throw new ArgumentOutOfRangeError;
    }
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new TakeSubscriber(subscriber, this.total);
  }
}
 
class TakeSubscriber<T> extends Subscriber<T> {
  private count: number = 0;
 
  constructor(destination: Subscriber<T>, private total: number) {
    super(destination);
  }
 
  _next(value: T): void {
    const total = this.total;
    Eif (++this.count <= total) {
      this.destination.next(value);
      if (this.count === total) {
        this.destination.complete();
      }
    }
  }
}