Code coverage report for src/operator/skipWhile.ts

Statements: 100% (30 / 30)      Branches: 100% (6 / 6)      Functions: 100% (7 / 7)      Lines: 100% (27 / 27)      Ignored: none     

All files » src/operator/ » skipWhile.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 44 45 46 47 48 49 50 51 52 53 54 55    1 1 1                       1 15     1 15     1 15   1   1 15 15   1 15 15     1 58 58 33 33 33 1   32     58 34     1  
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
/**
 * Returns an Observable that skips all items emitted by the source Observable as long as a specified condition holds
 * true, but emits all further source items as soon as the condition becomes false.
 *
 * <img src="./img/skipWhile.png" width="100%">
 *
 * @param {Function} predicate - a function to test each item emitted from the source Observable.
 * @returns {Observable<T>} an Observable that begins emitting items emitted by the source Observable when the
 * specified predicate becomes false.
 */
export function skipWhile<T>(predicate: (value: T, index: number) => boolean): Observable<T> {
  return this.lift(new SkipWhileOperator(predicate));
}
 
class SkipWhileOperator<T, R> implements Operator<T, R> {
  constructor(private predicate: (value: T, index: number) => boolean) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new SkipWhileSubscriber(subscriber, this.predicate);
  }
}
 
class SkipWhileSubscriber<T> extends Subscriber<T> {
  private skipping: boolean = true;
  private index: number = 0;
 
  constructor(destination: Subscriber<T>,
              private predicate: (value: T, index: number) => boolean) {
    super(destination);
  }
 
  protected _next(value: T): void {
    const destination = this.destination;
    if (this.skipping === true) {
      const index = this.index++;
      const result = tryCatch(this.predicate)(value, index);
      if (result === errorObject) {
        destination.error(errorObject.e);
      } else {
        this.skipping = Boolean(result);
      }
    }
    if (this.skipping === false) {
      destination.next(value);
    }
  }
}