Code coverage report for src/operator/switchFirst.ts

Statements: 100% (33 / 33)      Branches: 100% (6 / 6)      Functions: 100% (10 / 10)      Lines: 100% (31 / 31)      Ignored: none     

All files » src/operator/ » switchFirst.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        1 1   1 15     2 1 15   1   1 15 15   1 15     1 21 12 12       1 9 9 4       1 26     1 2     1 8 8 8 3     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 switchFirst<T>(): Observable<T> {
  return this.lift(new SwitchFirstOperator());
}
 
class SwitchFirstOperator<T, R> implements Operator<T, R> {
  call(subscriber: Subscriber<R>): Subscriber<T> {
    return new SwitchFirstSubscriber(subscriber);
  }
}
 
class SwitchFirstSubscriber<T, R> extends OuterSubscriber<T, R> {
  private hasSubscription: boolean = false;
  private hasCompleted: boolean = false;
 
  constructor(destination: Subscriber<R>) {
    super(destination);
  }
 
  _next(value: T): void {
    if (!this.hasSubscription) {
      this.hasSubscription = true;
      this.add(subscribeToResult(this, value));
    }
  }
 
  _complete(): void {
    this.hasCompleted = true;
    if (!this.hasSubscription) {
      this.destination.complete();
    }
  }
 
  notifyNext(outerValue: T, innerValue: any): void {
    this.destination.next(innerValue);
  }
 
  notifyError(err: any): void {
    this.destination.error(err);
  }
 
  notifyComplete(innerSub: Subscription<T>): void {
    this.remove(innerSub);
    this.hasSubscription = false;
    if (this.hasCompleted) {
      this.destination.complete();
    }
  }
}