Code coverage report for src/operator/windowWhen.ts

Statements: 100% (70 / 70)      Branches: 87.5% (7 / 8)      Functions: 100% (17 / 17)      Lines: 100% (64 / 64)      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 105 106 107 108  1   1 1   1 1   1 16     1   16     1 16   1   1       16 16 16 16     1 67     1 5 5 5     1 6 6 6     1 16 16     1 27 27 27       1 33 33 17 17     33 33 17     33 33   33 33 1 1 1   32 32 32 32     1   1 32 32     1 13     1 3     1 4   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> implements Operator<T, Observable<T>> {
 
  constructor(private closingSelector: () => Observable<any>) {
  }
 
  call(subscriber: Subscriber<Observable<T>>): Subscriber<T> {
    return new WindowSubscriber(subscriber, this.closingSelector);
  }
}
 
class WindowSubscriber<T> extends Subscriber<T> {
  private window: Subject<T>;
  private closingNotification: Subscription;
 
  constructor(protected destination: Subscriber<Observable<T>>,
              private closingSelector: () => Observable<any>) {
    super(destination);
    this.openWindow();
  }
 
  protected _next(value: T) {
    this.window.next(value);
  }
 
  protected _error(err: any) {
    this.window.error(err);
    this.destination.error(err);
    this._unsubscribeClosingNotification();
  }
 
  protected _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;
    if (prevWindow) {
      prevWindow.complete();
    }
 
    const window = this.window = new Subject<T>();
    this.destination.next(window);
 
    const closingNotifier = tryCatch(this.closingSelector)();
    if (closingNotifier === errorObject) {
      const err = errorObject.e;
      this.destination.error(err);
      this.window.error(err);
    } else {
      const closingNotification = this.closingNotification = new Subscription();
      closingNotification.add(closingNotifier.subscribe(new WindowClosingNotifierSubscriber(this)));
      this.add(closingNotification);
      this.add(window);
    }
  }
}
 
class WindowClosingNotifierSubscriber extends Subscriber<any> {
  constructor(private parent: WindowSubscriber<any>) {
    super();
  }
 
  protected _next() {
    this.parent.openWindow();
  }
 
  protected _error(err: any) {
    this.parent.error(err);
  }
 
  protected _complete() {
    this.parent.openWindow();
  }
}