Code coverage report for src/operator/sampleTime.ts

Statements: 100% (32 / 32)      Branches: 75% (3 / 4)      Functions: 100% (9 / 9)      Lines: 100% (25 / 25)      Ignored: none     

All files » src/operator/ » sampleTime.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    1   1   7 7     1 7     1 7   1   1   7   7 7 7     1 12 12     1 18 5     1   1 18 18 18    
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Scheduler} from '../Scheduler';
import {asap} from '../scheduler/asap';
 
export function sampleTime<T>(delay: number, Ischeduler: Scheduler = asap): Observable<T> {
  return this.lift(new SampleTimeOperator(delay, scheduler));
}
 
class SampleTimeOperator<T, R> implements Operator<T, R> {
  constructor(private delay: number, private scheduler: Scheduler) {
  }
 
  call(subscriber: Subscriber<R>) {
    return new SampleTimeSubscriber(subscriber, this.delay, this.scheduler);
  }
}
 
class SampleTimeSubscriber<T> extends Subscriber<T> {
  lastValue: T;
  hasValue: boolean = false;
 
  constructor(destination: Subscriber<T>, private delay: number, private scheduler: Scheduler) {
    super(destination);
    this.add(scheduler.schedule(dispatchNotification, delay, { subscriber: this, delay }));
  }
 
  _next(value: T) {
    this.lastValue = value;
    this.hasValue = true;
  }
 
  notifyNext() {
    if (this.hasValue) {
      this.destination.next(this.lastValue);
    }
  }
}
 
function dispatchNotification<T>(state) {
  let { subscriber, delay } = state;
  subscriber.notifyNext();
  (<any>this).schedule(state, delay);
}