Code coverage report for src/operator/window.ts

Statements: 100% (52 / 52)      Branches: 100% (2 / 2)      Functions: 100% (17 / 17)      Lines: 100% (46 / 46)      Ignored: none     

All files » src/operator/ » window.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  1   1   1 14     1   14     1 14   1   1     14 14 14 14 14     1 43     1 3 3     1 7 7     1 28 28 14   28 28 28 28     1 1     1 3   1   1 14 14     1 14     1 1     1 3   1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {Subject} from '../Subject';
 
export function window<T>(closingNotifier: Observable<any>): Observable<Observable<T>> {
  return this.lift(new WindowOperator(closingNotifier));
}
 
class WindowOperator<T> implements Operator<T, Observable<T>> {
 
  constructor(private closingNotifier: Observable<any>) {
  }
 
  call(subscriber: Subscriber<Observable<T>>): Subscriber<T> {
    return new WindowSubscriber(subscriber, this.closingNotifier);
  }
}
 
class WindowSubscriber<T> extends Subscriber<T> {
  private window: Subject<T>;
 
  constructor(protected destination: Subscriber<Observable<T>>,
              private closingNotifier: Observable<any>) {
    super(destination);
    this.add(closingNotifier.subscribe(new WindowClosingNotifierSubscriber(this)));
    this.openWindow();
  }
 
  protected _next(value: T) {
    this.window.next(value);
  }
 
  protected _error(err: any) {
    this.window.error(err);
    this.destination.error(err);
  }
 
  protected _complete() {
    this.window.complete();
    this.destination.complete();
  }
 
  openWindow() {
    const prevWindow = this.window;
    if (prevWindow) {
      prevWindow.complete();
    }
    const destination = this.destination;
    const newWindow = this.window = new Subject<T>();
    destination.add(newWindow);
    destination.next(newWindow);
  }
 
  errorWindow(err: any) {
    this._error(err);
  }
 
  completeWindow() {
    this._complete();
  }
}
 
class WindowClosingNotifierSubscriber extends Subscriber<any> {
  constructor(private parent: WindowSubscriber<any>) {
    super();
  }
 
  protected _next() {
    this.parent.openWindow();
  }
 
  protected _error(err: any) {
    this.parent.errorWindow(err);
  }
 
  protected _complete() {
    this.parent.completeWindow();
  }
}