Code coverage report for src/operator/throttleTime.ts

Statements: 100% (32 / 32)      Branches: 83.33% (5 / 6)      Functions: 100% (9 / 9)      Lines: 100% (27 / 27)      Ignored: none     

All files » src/operator/ » throttleTime.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  1     1     14 13     1 13     1 13   1   1     1 13 13 13     1 58 20 20       1 15 15 15 15 15     1   1 15    
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {Scheduler} from '../Scheduler';
import {Subscription} from '../Subscription';
import {asap} from '../scheduler/asap';
import {Observable} from '../Observable';
 
export function throttleTime<T>(delay: number, scheduler: Scheduler = asap): Observable<T> {
  return this.lift(new ThrottleTimeOperator(delay, scheduler));
}
 
class ThrottleTimeOperator<T> implements Operator<T, T> {
  constructor(private delay: number, private scheduler: Scheduler) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new ThrottleTimeSubscriber(subscriber, this.delay, this.scheduler);
  }
}
 
class ThrottleTimeSubscriber<T> extends Subscriber<T> {
  private throttled: Subscription;
 
  constructor(destination: Subscriber<T>,
              private delay: number,
              private scheduler: Scheduler) {
    super(destination);
  }
 
  protected _next(value: T) {
    if (!this.throttled) {
      this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.delay, { subscriber: this }));
      this.destination.next(value);
    }
  }
 
  clearThrottle() {
    const throttled = this.throttled;
    Eif (throttled) {
      throttled.unsubscribe();
      this.remove(throttled);
      this.throttled = null;
    }
  }
}
 
function dispatchNext<T>({ subscriber }) {
  subscriber.clearThrottle();
}