Code coverage report for src/operator/timeInterval.ts

Statements: 100% (31 / 31)      Branches: 50% (1 / 2)      Functions: 100% (9 / 9)      Lines: 100% (23 / 23)      Ignored: none     

All files » src/operator/ » timeInterval.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    1   1   11 11     1 28     1   1 11       1 11   1   1 11   11 11   11     1 16 16 16   16   1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Scheduler} from '../Scheduler';
import {asap} from '../scheduler/asap';
 
export function timeInterval<T>(Ischeduler: Scheduler = asap): Observable<TimeInterval<T>> {
  return this.lift(new TimeIntervalOperator(scheduler));
}
 
export class TimeInterval<T> {
  constructor(public value: T, public interval: number) {
 
  }
};
 
class TimeIntervalOperator<T> implements Operator<T, TimeInterval<T>> {
  constructor(private scheduler: Scheduler) {
 
  }
 
  call(observer: Subscriber<TimeInterval<T>>): Subscriber<T> {
    return new TimeIntervalSubscriber(observer, this.scheduler);
  }
}
 
class TimeIntervalSubscriber<T> extends Subscriber<T> {
  private lastTime: number = 0;
 
  constructor(destination: Subscriber<TimeInterval<T>>, private scheduler: Scheduler) {
    super(destination);
 
    this.lastTime = scheduler.now();
  }
 
  protected _next(value: T) {
    let now = this.scheduler.now();
    let span = now - this.lastTime;
    this.lastTime = now;
 
    this.destination.next(new TimeInterval(value, span));
  }
}