Code coverage report for src/operator/race.ts

Statements: 94.64% (53 / 56)      Branches: 78.57% (11 / 14)      Functions: 100% (9 / 9)      Lines: 93.88% (46 / 49)      Ignored: none     

All files » src/operator/ » race.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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101  1 1       1 1               20     10       10 10               68     24 4     4       20     2 1 20   1   1 20 20 20   1 20     1 40     1 20 20 20     20 40 40   40 40   20       1 34 14   14 28 14   14 14       14     34   1  
import {Observable} from '../Observable';
import {isArray} from '../util/isArray';
import {ArrayObservable} from '../observable/ArrayObservable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
/**
 * Returns an Observable that mirrors the first source Observable to emit an item
 * from the combination of this Observable and supplied Observables
 * @param {...Observables} ...observables sources used to race for which Observable emits first.
 * @returns {Observable} an Observable that mirrors the output of the first Observable to emit an item.
 */
export function race<T>(...observables: Array<Observable<T> | Array<Observable<T>>>): Observable<T> {
  // if the only argument is an array, it was most likely called with
  // `pair([obs1, obs2, ...])`
  Iif (observables.length === 1 && isArray(observables[0])) {
    observables = <Array<Observable<T>>>observables[0];
  }
 
  observables.unshift(this);
  return raceStatic.apply(this, observables);
}
 
/**
 * Returns an Observable that mirrors the first source Observable to emit an item.
 * @param {...Observables} ...observables sources used to race for which Observable emits first.
 * @returns {Observable} an Observable that mirrors the output of the first Observable to emit an item.
 */
export function raceStatic<T>(...observables: Array<Observable<T> | Array<Observable<T>>>): Observable<T> {
  // if the only argument is an array, it was most likely called with
  // `pair([obs1, obs2, ...])`
  if (observables.length === 1) {
    Iif (isArray(observables[0])) {
      observables = <Array<Observable<any>>>observables[0];
    } else {
      return <Observable<T>>observables[0];
    }
  }
 
  return new ArrayObservable(observables).lift(new RaceOperator());
}
 
export class RaceOperator<T, R> implements Operator<T, R> {
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new RaceSubscriber(subscriber);
  }
}
 
export class RaceSubscriber<T, R> extends OuterSubscriber<T, R> {
  private hasFirst: boolean = false;
  private observables: Observable<any>[] = [];
  private subscriptions: Subscription[] = [];
 
  constructor(destination: Subscriber<T>) {
    super(destination);
  }
 
  protected _next(observable: any): void {
    this.observables.push(observable);
  }
 
  protected _complete() {
    const observables = this.observables;
    const len = observables.length;
    Iif (len === 0) {
      this.destination.complete();
    } else {
      for (let i = 0; i < len; i++) {
        let observable = observables[i];
        let subscription = subscribeToResult(this, observable, observable, i);
 
        this.subscriptions.push(subscription);
        this.add(subscription);
      }
      this.observables = null;
    }
  }
 
  notifyNext(observable: any, value: R, outerIndex: number): void {
    if (!this.hasFirst) {
      this.hasFirst = true;
 
      for (let i = 0; i < this.subscriptions.length; i++) {
        if (i !== outerIndex) {
          let subscription = this.subscriptions[i];
 
          subscription.unsubscribe();
          this.remove(subscription);
        }
      }
 
      this.subscriptions = null;
    }
 
    this.destination.next(value);
  }
}