Code coverage report for src/operator/windowToggle.ts

Statements: 100% (90 / 90)      Branches: 78.57% (11 / 14)      Functions: 100% (14 / 14)      Lines: 100% (87 / 87)      Ignored: none     

All files » src/operator/ » windowToggle.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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149      1 1   1 1   1 1   1   18     1   18 18     1 18       1             1 18     1 18 18 18 18     1 75 75 75 75 69         1   6 6   6 6 6   6 5 5 5       6     1 8 8 8 8 8 8 7 7 7     8     1 18 18 18 4 4 4 4 4 4         1   49   35 35   35 1   34 34 34 34 34 34 34   34     14       1 3     1 9 4       1 18 18 18 18 18 18   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';
 
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
export function windowToggle<T, O>(openings: Observable<O>,
                                   closingSelector: (openValue: O) => Observable<any>): Observable<Observable<T>> {
  return this.lift(new WindowToggleOperator(openings, closingSelector));
}
 
class WindowToggleOperator<T, O> implements Operator<T, Observable<T>> {
 
  constructor(private openings: Observable<O>,
              private closingSelector: (openValue: O) => Observable<any>) {
  }
 
  call(subscriber: Subscriber<Observable<T>>): Subscriber<T> {
    return new WindowToggleSubscriber(
      subscriber, this.openings, this.closingSelector
    );
  }
}
 
interface WindowContext<T> {
  window: Subject<T>;
  subscription: Subscription;
}
 
class WindowToggleSubscriber<T, R, O> extends OuterSubscriber<T, R> {
  private contexts: WindowContext<T>[] = [];
  private openSubscription: Subscription;
 
  constructor(destination: Subscriber<Observable<T>>,
              private openings: Observable<O>,
              private closingSelector: (openValue: O) => Observable<any>) {
    super(destination);
    this.add(this.openSubscription = subscribeToResult(this, openings, openings));
  }
 
  protected _next(value: T) {
    const { contexts } = this;
    Eif (contexts) {
      const len = contexts.length;
      for (let i = 0; i < len; i++) {
        contexts[i].window.next(value);
      }
    }
  }
 
  protected _error(err: any) {
 
    const { contexts } = this;
    this.contexts = null;
 
    Eif (contexts) {
      const len = contexts.length;
      let index = -1;
 
      while (++index < len) {
        const context = contexts[index];
        context.window.error(err);
        context.subscription.unsubscribe();
      }
    }
 
    super._error(err);
  }
 
  protected _complete() {
    const { contexts } = this;
    this.contexts = null;
    Eif (contexts) {
      const len = contexts.length;
      let index = -1;
      while (++index < len) {
        const context = contexts[index];
        context.window.complete();
        context.subscription.unsubscribe();
      }
    }
    super._complete();
  }
 
  _unsubscribe() {
    const { contexts } = this;
    this.contexts = null;
    if (contexts) {
      const len = contexts.length;
      let index = -1;
      while (++index < len) {
        const context = contexts[index];
        context.window.unsubscribe();
        context.subscription.unsubscribe();
      }
    }
  }
 
  notifyNext(outerValue: any, innerValue: any, outerIndex: number, innerIndex: number): void {
 
    if (outerValue === this.openings) {
 
      const { closingSelector } = this;
      const closingNotifier = tryCatch(closingSelector)(innerValue);
 
      if (closingNotifier === errorObject) {
        return this.error(errorObject.e);
      } else {
        const window = new Subject<T>();
        const subscription = new Subscription();
        const context = { window, subscription };
        this.contexts.push(context);
        const innerSubscription = subscribeToResult(this, closingNotifier, context);
        (<any> innerSubscription).context = context;
        subscription.add(innerSubscription);
 
        this.destination.next(window);
      }
    } else {
      this.closeWindow(this.contexts.indexOf(outerValue));
    }
  }
 
  notifyError(err: any): void {
    this.error(err);
  }
 
  notifyComplete(inner: Subscription): void {
    if (inner !== this.openSubscription) {
      this.closeWindow(this.contexts.indexOf((<any> inner).context));
    }
  }
 
  closeWindow(index: number) {
    const { contexts } = this;
    const context = contexts[index];
    const { window, subscription } = context;
    contexts.splice(index, 1);
    window.complete();
    subscription.unsubscribe();
  }
}