Code coverage report for src/operator/debounce.ts

Statements: 100% (69 / 69)      Branches: 100% (12 / 12)      Functions: 100% (17 / 17)      Lines: 100% (64 / 64)      Ignored: none     

All files » src/operator/ » debounce.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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108    1 1     1 1 1   1 24     1 24     1 24   1   1 24 24 24 1 75     1 24 24     1 89 89 89   89 1   88 7     88 88 88       1 13 13     1 70 70 59 59       1 158   158 30 30 30     1   1 88 88 88     1 75   75 57 57 34         1 48     1 2     1 27   1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {PromiseObservable} from '../observable/fromPromise';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
 
import {tryCatch} from '../util/tryCatch';
import {isPromise} from '../util/isPromise';
import {errorObject} from '../util/errorObject';
 
export function debounce<T>(durationSelector: (value: T) => Observable<any> | Promise<any>): Observable<T> {
  return this.lift(new DebounceOperator(durationSelector));
}
 
class DebounceOperator<T, R> implements Operator<T, R> {
  constructor(private durationSelector: (value: T) => Observable<any> | Promise<any>) {
  }
 
  call(observer: Subscriber<T>): Subscriber<T> {
    return new DebounceSubscriber(observer, this.durationSelector);
  }
}
 
class DebounceSubscriber<T> extends Subscriber<T> {
  private debouncedSubscription: Subscription<any> = null;
  private lastValue: any = null;
  private _index: number = 0;
  get index() {
    return this._index;
  }
 
  constructor(destination: Subscriber<T>,
              private durationSelector: (value: T) => Observable<any> | Promise<any>) {
    super(destination);
  }
 
  _next(value: T) {
    const destination = this.destination;
    const currentIndex = ++this._index;
    let debounce = tryCatch(this.durationSelector)(value);
 
    if (debounce === errorObject) {
      destination.error(errorObject.e);
    } else {
      if (isPromise(debounce)) {
        debounce = PromiseObservable.create(debounce);
      }
 
      this.lastValue = value;
      this.clearDebounce();
      this.add(this.debouncedSubscription = debounce._subscribe(new DurationSelectorSubscriber(this, currentIndex)));
    }
  }
 
  _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) {
      debouncedSubscription.unsubscribe();
      this.remove(debouncedSubscription);
      this.debouncedSubscription = null;
    }
  }
}
 
class DurationSelectorSubscriber<T> extends Subscriber<T> {
  constructor(private parent: DebounceSubscriber<any>,
              private currentIndex: number) {
    super(null);
  }
 
  private debounceNext(): void {
    const parent = this.parent;
 
    if (this.currentIndex === parent.index) {
      parent.debouncedNext();
      if (!this.isUnsubscribed) {
        this.unsubscribe();
      }
    }
  }
 
  _next(unused: T) {
     this.debounceNext();
  }
 
  _error(err) {
    this.parent.error(err);
  }
 
  _complete() {
    this.debounceNext();
  }
}