Code coverage report for src/operator/mergeAll-support.ts

Statements: 100% (38 / 38)      Branches: 100% (14 / 14)      Functions: 100% (8 / 8)      Lines: 100% (33 / 33)      Ignored: none     

All files » src/operator/ » mergeAll-support.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 181     1 181   1   1 181 181 181   181 181     1 471 363 37   326 326     108       1 168 168 10       1 225 225 225 225 81 144 87     1
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Observer} from '../Observer';
import {Subscription} from '../Subscription';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
export class MergeAllOperator<T, R> implements Operator<T, R> {
  constructor(private concurrent: number) {
  }
 
  call(observer: Observer<T>) {
    return new MergeAllSubscriber(observer, this.concurrent);
  }
}
 
export class MergeAllSubscriber<T, R> extends OuterSubscriber<T, R> {
  private hasCompleted: boolean = false;
  private buffer: Observable<any>[] = [];
  private active: number = 0;
 
  constructor(destination: Observer<T>, private concurrent: number) {
    super(destination);
  }
 
  _next(observable: any) {
    if (this.active < this.concurrent) {
      if (observable._isScalar) {
        this.destination.next(observable.value);
      } else {
        this.active++;
        this.add(subscribeToResult<T, R>(this, observable));
      }
    } else {
      this.buffer.push(observable);
    }
  }
 
  _complete() {
    this.hasCompleted = true;
    if (this.active === 0 && this.buffer.length === 0) {
      this.destination.complete();
    }
  }
 
  notifyComplete(innerSub: Subscription) {
    const buffer = this.buffer;
    this.remove(innerSub);
    this.active--;
    if (buffer.length > 0) {
      this._next(buffer.shift());
    } else if (this.active === 0 && this.hasCompleted) {
      this.destination.complete();
    }
  }
}