Code coverage report for src/operator/switchMapTo.ts

Statements: 100% (52 / 52)      Branches: 100% (14 / 14)      Functions: 100% (11 / 11)      Lines: 100% (49 / 49)      Ignored: none     

All files » src/operator/ » switchMapTo.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        1 1 1 1   1         17     1 17 17           1 17   1   1   17 17   1 17 17 17     1 25 25 25 10   25     1 10 10 10 6       1 11 11 11 9   11   11 2       1 2     1 52 52 9 9 1   8     43     1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
export function switchMapTo<T, R, R2>(observable: Observable<R>,
                                      projectResult?: (outerValue: T,
                                                       innerValue: R,
                                                       outerIndex: number,
                                                       innerIndex: number) => R2): Observable<R2> {
  return this.lift(new SwitchMapToOperator(observable, projectResult));
}
 
class SwitchMapToOperator<T, R, R2> implements Operator<T, R> {
  constructor(private observable: Observable<R>,
              private resultSelector?: (outerValue: T,
                                        innerValue: R,
                                        outerIndex: number,
                                        innerIndex: number) => R2) {
  }
 
  call(subscriber: Subscriber<R>): Subscriber<T> {
    return new SwitchMapToSubscriber(subscriber, this.observable, this.resultSelector);
  }
}
 
class SwitchMapToSubscriber<T, R, R2> extends OuterSubscriber<T, R> {
  private innerSubscription: Subscription<T>;
  private hasCompleted = false;
  index: number = 0;
 
  constructor(destination: Subscriber<R>,
              private inner: Observable<R>,
              private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2) {
    super(destination);
  }
 
  _next(value: any) {
    const index = this.index++;
    const innerSubscription = this.innerSubscription;
    if (innerSubscription) {
      innerSubscription.unsubscribe();
    }
    this.add(this.innerSubscription = subscribeToResult(this, this.inner, value, index));
  }
 
  _complete() {
    const innerSubscription = this.innerSubscription;
    this.hasCompleted = true;
    if (!innerSubscription || innerSubscription.isUnsubscribed) {
      this.destination.complete();
    }
  }
 
  notifyComplete(innerSub: Subscription<R>) {
    this.remove(innerSub);
    const prevSubscription = this.innerSubscription;
    if (prevSubscription) {
      prevSubscription.unsubscribe();
    }
    this.innerSubscription = null;
 
    if (this.hasCompleted) {
      this.destination.complete();
    }
  }
 
  notifyError(err: any) {
    this.destination.error(err);
  }
 
  notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) {
    const { resultSelector, destination } = this;
    if (resultSelector) {
      const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex);
      if (result === errorObject) {
        destination.error(errorObject.e);
      } else {
        destination.next(result);
      }
    } else {
      destination.next(innerValue);
    }
  }
}