Code coverage report for src/operator/switch.ts

Statements: 100% (38 / 38)      Branches: 100% (10 / 10)      Functions: 100% (11 / 11)      Lines: 100% (36 / 36)      Ignored: none     

All files » src/operator/ » switch.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        1 1   1 15     2 1 15   1   1 15 15     1 15     1 27 27 27     1 11 11 5       1 37 37 37 23 23       1 44     1 1     1 10 10 5     1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
export function _switch<T>(): Observable<T> {
  return this.lift(new SwitchOperator());
}
 
class SwitchOperator<T, R> implements Operator<T, R> {
  call(subscriber: Subscriber<R>): Subscriber<T> {
    return new SwitchSubscriber(subscriber);
  }
}
 
class SwitchSubscriber<T, R> extends OuterSubscriber<T, R> {
  private active: number = 0;
  private hasCompleted: boolean = false;
  innerSubscription: Subscription<T>;
 
  constructor(destination: Subscriber<R>) {
    super(destination);
  }
 
  _next(value: T): void {
    this.unsubscribeInner();
    this.active++;
    this.add(this.innerSubscription = subscribeToResult(this, value));
  }
 
  _complete(): void {
    this.hasCompleted = true;
    if (this.active === 0) {
      this.destination.complete();
    }
  }
 
  private unsubscribeInner(): void {
    this.active = this.active > 0 ? this.active - 1 : 0;
    const innerSubscription = this.innerSubscription;
    if (innerSubscription) {
      innerSubscription.unsubscribe();
      this.remove(innerSubscription);
    }
  }
 
  notifyNext(outerValue: T, innerValue: any): void {
    this.destination.next(innerValue);
  }
 
  notifyError(err: any): void {
    this.destination.error(err);
  }
 
  notifyComplete(): void {
    this.unsubscribeInner();
    if (this.hasCompleted && this.active === 0) {
      this.destination.complete();
    }
  }
}