Code coverage report for src/operator/combineLatest-support.ts

Statements: 98.18% (54 / 55)      Branches: 92.86% (13 / 14)      Functions: 100% (9 / 9)      Lines: 98% (49 / 50)      Ignored: none     

All files » src/operator/ » combineLatest-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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83      1 1 1 1   1 88     1 88   1   1 88 88 88 88   88 88     1 178 178 178     1 88 88 88     88 88 178 178         1 73 25       1 131 131 131   131 75 75 64       131 78 78   78 59 59 3   56     19       1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
 
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
export class CombineLatestOperator<T, R> implements Operator<T, R> {
  constructor(private project?: (...values: Array<any>) => R) {
  }
 
  call(subscriber: Subscriber<R>): Subscriber<T> {
    return new CombineLatestSubscriber<T, R>(subscriber, this.project);
  }
}
 
export class CombineLatestSubscriber<T, R> extends OuterSubscriber<T, R> {
  private active: number = 0;
  private values: any[] = [];
  private observables: any[] = [];
  private toRespond: number[] = [];
 
  constructor(destination: Subscriber<R>, private project?: (...values: Array<any>) => R) {
    super(destination);
  }
 
  _next(observable: any) {
    const toRespond = this.toRespond;
    toRespond.push(toRespond.length);
    this.observables.push(observable);
  }
 
  _complete() {
    const observables = this.observables;
    const len = observables.length;
    Iif (len === 0) {
      this.destination.complete();
    } else {
      this.active = len;
      for (let i = 0; i < len; i++) {
        const observable = observables[i];
        this.add(subscribeToResult(this, observable, observable, i));
      }
    }
  }
 
  notifyComplete(unused: Subscriber<R>): void {
    if ((this.active -= 1) === 0) {
      this.destination.complete();
    }
  }
 
  notifyNext(observable: any, value: R, outerIndex: number, innerIndex: number) {
    const values = this.values;
    values[outerIndex] = value;
    const toRespond = this.toRespond;
 
    if (toRespond.length > 0) {
      const found = toRespond.indexOf(outerIndex);
      if (found !== -1) {
        toRespond.splice(found, 1);
      }
    }
 
    if (toRespond.length === 0) {
      const project = this.project;
      const destination = this.destination;
 
      if (project) {
        const result = tryCatch(project).apply(this, values);
        if (result === errorObject) {
          destination.error(errorObject.e);
        } else {
          destination.next(result);
        }
      } else {
        destination.next(values);
      }
    }
  }
}