Code coverage report for src/operator/debounceTime.ts

Statements: 100% (42 / 42)      Branches: 83.33% (5 / 6)      Functions: 100% (11 / 11)      Lines: 100% (37 / 37)      Ignored: none     

All files » src/operator/ » debounceTime.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    1     1   12 12     1 12     1 12   1   1 12 12   1 12 12 12     1 30 30 30     1 5 5     1 13 13 10 10       1 43   43 28 28 28     1   1 8    
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Scheduler} from '../Scheduler';
import {Subscription} from '../Subscription';
import {asap} from '../scheduler/asap';
 
export function debounceTime<T>(dueTime: number, Ischeduler: Scheduler = asap): Observable<T> {
  return this.lift(new DebounceTimeOperator(dueTime, scheduler));
}
 
class DebounceTimeOperator<T, R> implements Operator<T, R> {
  constructor(private dueTime: number, private scheduler: Scheduler) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new DebounceTimeSubscriber(subscriber, this.dueTime, this.scheduler);
  }
}
 
class DebounceTimeSubscriber<T> extends Subscriber<T> {
  private debouncedSubscription: Subscription<any> = null;
  private lastValue: any = null;
 
  constructor(destination: Subscriber<T>,
              private dueTime: number,
              private scheduler: Scheduler) {
    super(destination);
  }
 
  _next(value: T) {
    this.clearDebounce();
    this.lastValue = value;
    this.add(this.debouncedSubscription = this.scheduler.schedule(dispatchNext, this.dueTime, this));
  }
 
  _complete() {
    this.debouncedNext();
    this.destination.complete();
  }
 
  debouncedNext(): void {
    this.clearDebounce();
    if (this.lastValue != null) {
      this.destination.next(this.lastValue);
      this.lastValue = null;
    }
  }
 
  private clearDebounce(): void {
    const debouncedSubscription = this.debouncedSubscription;
 
    if (debouncedSubscription !== null) {
      this.remove(debouncedSubscription);
      debouncedSubscription.unsubscribe();
      this.debouncedSubscription = null;
    }
  }
}
 
function dispatchNext(subscriber) {
  subscriber.debouncedNext();
}