Code coverage report for src/operator/windowTime.ts

Statements: 100% (80 / 80)      Branches: 80% (12 / 15)      Functions: 100% (14 / 14)      Lines: 100% (76 / 76)      Ignored: none     

All files » src/operator/ » windowTime.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  1   1     1   1 11 11 11     1   11 11 11     1 11       1   1 11   11 11 11 11 11 11 10 10 10 10 10   1 1 1       1 43 43 43 38       1 2 2 1   2     1 5 5 5   5     1 24 24 24 24 24 24     1 14 14 14   1               1 2 2 2   2 2     1 11 11 11 11 11 11 11 11     1 14 8   14    
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {Subject} from '../Subject';
import {Scheduler} from '../Scheduler';
import {Action} from '../scheduler/Action';
import {asap} from '../scheduler/asap';
 
export function windowTime<T>(windowTimeSpan: number,
                              IwindowCreationInterval: number = null,
                              Ischeduler: Scheduler = asap): Observable<Observable<T>> {
  return this.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, scheduler));
}
 
class WindowTimeOperator<T> implements Operator<T, Observable<T>> {
 
  constructor(private windowTimeSpan: number,
              private windowCreationInterval: number,
              private scheduler: Scheduler) {
  }
 
  call(subscriber: Subscriber<Observable<T>>): Subscriber<T> {
    return new WindowTimeSubscriber(
      subscriber, this.windowTimeSpan, this.windowCreationInterval, this.scheduler
    );
  }
}
 
class WindowTimeSubscriber<T> extends Subscriber<T> {
  private windows: Subject<T>[] = [];
 
  constructor(protected destination: Subscriber<Observable<T>>,
              private windowTimeSpan: number,
              private windowCreationInterval: number,
              private scheduler: Scheduler) {
    super(destination);
    if (windowCreationInterval !== null && windowCreationInterval >= 0) {
      let window = this.openWindow();
      const closeState = { subscriber: this, window, context: <any>null };
      const creationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler };
      this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
      this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
    } else {
      let window = this.openWindow();
      const timeSpanOnlyState = { subscriber: this, window, windowTimeSpan };
      this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
    }
  }
 
  protected _next(value: T) {
    const windows = this.windows;
    const len = windows.length;
    for (let i = 0; i < len; i++) {
      windows[i].next(value);
    }
  }
 
  protected _error(err: any) {
    const windows = this.windows;
    while (windows.length > 0) {
      windows.shift().error(err);
    }
    this.destination.error(err);
  }
 
  protected _complete() {
    const windows = this.windows;
    while (windows.length > 0) {
      windows.shift().complete();
    }
    this.destination.complete();
  }
 
  openWindow(): Subject<T> {
    const window = new Subject<T>();
    this.windows.push(window);
    const destination = this.destination;
    destination.add(window);
    destination.next(window);
    return window;
  }
 
  closeWindow(window: Subject<T>) {
    window.complete();
    const windows = this.windows;
    windows.splice(windows.indexOf(window), 1);
  }
}
 
interface TimeSpanOnlyState<T> {
  window: Subject<any>;
  windowTimeSpan: number;
  subscriber: WindowTimeSubscriber<T>;
}
 
function dispatchWindowTimeSpanOnly<T>(state: TimeSpanOnlyState<T>) {
  const { subscriber, windowTimeSpan, window } = state;
  Eif (window) {
    window.complete();
  }
  state.window = subscriber.openWindow();
  (<any>this).schedule(state, windowTimeSpan);
}
 
function dispatchWindowCreation(state: any) {
  let { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state;
  let window = subscriber.openWindow();
  let action = <Action>this;
  let context = { action, subscription: <any>null };
  const timeSpanState = { subscriber, window, context };
  context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
  action.add(context.subscription);
  action.schedule(state, windowCreationInterval);
}
 
function dispatchWindowClose({ subscriber, window, context }) {
  if (context && context.action && context.subscription) {
    context.action.remove(context.subscription);
  }
  subscriber.closeWindow(window);
}