Code coverage report for src/operator/windowWhen.ts

Statements: 100% (67 / 67)      Branches: 75% (6 / 8)      Functions: 100% (17 / 17)      Lines: 100% (61 / 61)      Ignored: none     

All files » src/operator/ » windowWhen.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 91 92 93 94 95 96 97 98 99 100 101 102 103 104  1   1 1   1 1   1 14     1   14     1 14   1   1 14     14 14 14     1 58     1 5 5 5     1 6 6 6     1 19 19     1 30 30 30       1 30 30 16 16     30 30 30     30   30 30 1 1 1   29 29     1   1 29 29     1 14     1 3     1 2   1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {Subject} from '../Subject';
import {Subscription} from '../Subscription';
 
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
export function windowWhen<T>(closingSelector: () => Observable<any>): Observable<Observable<T>> {
  return this.lift(new WindowOperator(closingSelector));
}
 
class WindowOperator<T, R> implements Operator<T, R> {
 
  constructor(private closingSelector: () => Observable<any>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new WindowSubscriber(subscriber, this.closingSelector);
  }
}
 
class WindowSubscriber<T> extends Subscriber<T> {
  private window: Subject<T> = new Subject<T>();
  private closingNotification: Subscription<any>;
 
  constructor(destination: Subscriber<T>, private closingSelector: () => Observable<any>) {
    super(destination);
    this.openWindow();
  }
 
  _next(value: T) {
    this.window.next(value);
  }
 
  _error(err: any) {
    this.window.error(err);
    this.destination.error(err);
    this._unsubscribeClosingNotification();
  }
 
  _complete() {
    this.window.complete();
    this.destination.complete();
    this._unsubscribeClosingNotification();
  }
 
  unsubscribe() {
    super.unsubscribe();
    this._unsubscribeClosingNotification();
  }
 
  _unsubscribeClosingNotification() {
    let closingNotification = this.closingNotification;
    Eif (closingNotification) {
      closingNotification.unsubscribe();
    }
  }
 
  openWindow() {
    const prevClosingNotification = this.closingNotification;
    if (prevClosingNotification) {
      this.remove(prevClosingNotification);
      prevClosingNotification.unsubscribe();
    }
 
    const prevWindow = this.window;
    Eif (prevWindow) {
      prevWindow.complete();
    }
 
    this.destination.next(this.window = new Subject<T>());
 
    let closingNotifier = tryCatch(this.closingSelector)();
    if (closingNotifier === errorObject) {
      const err = closingNotifier.e;
      this.destination.error(err);
      this.window.error(err);
    } else {
      let closingNotification = this.closingNotification = new Subscription();
      this.add(closingNotification.add(closingNotifier._subscribe(new WindowClosingNotifierSubscriber(this))));
    }
  }
}
 
class WindowClosingNotifierSubscriber<T> extends Subscriber<T> {
  constructor(private parent: WindowSubscriber<any>) {
    super(null);
  }
 
  _next() {
    this.parent.openWindow();
  }
 
  _error(err) {
    this.parent.error(err);
  }
 
  _complete() {
    this.parent.openWindow();
  }
}