Code coverage report for src/operator/exhaust.ts

Statements: 100% (29 / 29)      Branches: 100% (6 / 6)      Functions: 100% (8 / 8)      Lines: 100% (27 / 27)      Ignored: none     

All files » src/operator/ » exhaust.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        1 1                 1 17     2 1 17   1   1 17 17   1 17     1 26 15 15       1 10 10 4       1 10 10 10 4     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';
 
/**
 * Returns an Observable that takes a source of observables and propagates the first observable exclusively
 * until it completes before subscribing to the next.
 * Items that come in before the first has exhausted will be dropped.
 * Similar to `concatAll`, but will not hold on to items that come in before the first is exhausted.
 * @returns {Observable} an Observable which contains all of the items of the first Observable and following Observables in the source.
 */
export function exhaust<T>(): Observable<T> {
  return this.lift(new SwitchFirstOperator());
}
 
class SwitchFirstOperator<T> implements Operator<T, T> {
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new SwitchFirstSubscriber(subscriber);
  }
}
 
class SwitchFirstSubscriber<T> extends OuterSubscriber<T, T> {
  private hasCompleted: boolean = false;
  private hasSubscription: boolean = false;
 
  constructor(destination: Subscriber<T>) {
    super(destination);
  }
 
  protected _next(value: T): void {
    if (!this.hasSubscription) {
      this.hasSubscription = true;
      this.add(subscribeToResult(this, value));
    }
  }
 
  protected _complete(): void {
    this.hasCompleted = true;
    if (!this.hasSubscription) {
      this.destination.complete();
    }
  }
 
  notifyComplete(innerSub: Subscription): void {
    this.remove(innerSub);
    this.hasSubscription = false;
    if (this.hasCompleted) {
      this.destination.complete();
    }
  }
}