Code coverage report for src/operator/extended/find-support.ts

Statements: 100% (40 / 40)      Branches: 90% (9 / 10)      Functions: 100% (8 / 8)      Lines: 100% (36 / 36)      Ignored: none     

All files » src/operator/extended/ » find-support.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 56 57 58 59    1   1 1 1   1 12 12 12 12     1 12   1   1   12   1   12 12 12 12   12 12       1 6   6 6     1 14   14 14 14 2 12 2       1 4   1
import {Operator} from '../../Operator';
import {Observable} from '../../Observable';
import {Subscriber} from '../../Subscriber';
 
import {tryCatch} from '../../util/tryCatch';
import {errorObject} from '../../util/errorObject';
import {bindCallback} from '../../util/bindCallback';
 
export class FindValueOperator<T, R> implements Operator<T, R> {
  constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
              private source: Observable<T>,
              private yieldIndex: boolean,
              private thisArg?: any) {
  }
 
  call(observer: Subscriber<T>): Subscriber<T> {
    return new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg);
  }
}
 
export class FindValueSubscriber<T> extends Subscriber<T> {
  private predicate: Function;
  private index: number = 0;
 
  constructor(destination: Subscriber<T>,
              predicate: (value: T, index: number, source: Observable<T>) => boolean,
              private source: Observable<T>,
              private yieldIndex: boolean,
              private thisArg?: any) {
    super(destination);
 
    Eif (typeof predicate === 'function') {
      this.predicate = bindCallback(predicate, thisArg, 3);
    }
  }
 
  private notifyComplete(value: any): void {
    const destination = this.destination;
 
    destination.next(value);
    destination.complete();
  }
 
  _next(value: T): void {
    const predicate = this.predicate;
 
    let index = this.index++;
    let result = tryCatch(predicate)(value, index, this.source);
    if (result === errorObject) {
      this.destination.error(result.e);
    } else if (result) {
      this.notifyComplete(this.yieldIndex ? index : value);
    }
  }
 
  _complete(): void {
    this.notifyComplete(this.yieldIndex ? -1 : undefined);
  }
}