Code coverage report for src/operator/extended/timeInterval.ts

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

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