Code coverage report for src/operator/takeWhile.ts

Statements: 100% (27 / 27)      Branches: 100% (4 / 4)      Functions: 100% (7 / 7)      Lines: 100% (24 / 24)      Ignored: none     

All files » src/operator/ » takeWhile.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    1 1 1   1 17     1 17     1 17   1   1 17   1 17 17     1 61 61   61 1 60 54   6     1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
export function takeWhile<T>(predicate: (value: T, index: number) => boolean): Observable<T> {
  return this.lift(new TakeWhileOperator(predicate));
}
 
class TakeWhileOperator<T, R> implements Operator<T, R> {
  constructor(private predicate: (value: T, index: number) => boolean) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new TakeWhileSubscriber(subscriber, this.predicate);
  }
}
 
class TakeWhileSubscriber<T> extends Subscriber<T> {
  private index: number = 0;
 
  constructor(destination: Subscriber<T>,
              private predicate: (value: T, index: number) => boolean) {
    super(destination);
  }
 
  _next(value: T): void {
    const destination = this.destination;
    const result = tryCatch(this.predicate)(value, this.index++);
 
    if (result == errorObject) {
      destination.error(result.e);
    } else if (Boolean(result)) {
      destination.next(value);
    } else {
      destination.complete();
    }
  }
}