Code coverage report for src/operator/debounce.ts

Statements: 100% (52 / 52)      Branches: 100% (10 / 10)      Functions: 100% (11 / 11)      Lines: 100% (49 / 49)      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          1 1   1 1                       1 27     1 27     1 27   1   1     27 27   1 27 27     1 99 99   99 1   98 98 98 29 29   98 98 86         1 14 14     1 38     1 21     1 73 63 63 63 51 51 51   63 63 63     1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {Subscription} from '../Subscription';
 
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
 
/**
 * Returns the source Observable delayed by the computed debounce duration,
 * with the duration lengthened if a new source item arrives before the delay
 * duration ends.
 * In practice, for each item emitted on the source, this operator holds the
 * latest item, waits for a silence as long as the `durationSelector` specifies,
 * and only then emits the latest source item on the result Observable.
 * @param {function} durationSelector function for computing the timeout duration for each item.
 * @returns {Observable} an Observable the same as source Observable, but drops items.
 */
export function debounce<T>(durationSelector: (value: T) => Observable<number> | Promise<number>): Observable<T> {
  return this.lift(new DebounceOperator(durationSelector));
}
 
class DebounceOperator<T> implements Operator<T, T> {
  constructor(private durationSelector: (value: T) => Observable<number> | Promise<number>) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new DebounceSubscriber(subscriber, this.durationSelector);
  }
}
 
class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
 
  private value: T;
  private hasValue: boolean = false;
  private durationSubscription: Subscription = null;
 
  constructor(destination: Subscriber<R>,
              private durationSelector: (value: T) => Observable<number> | Promise<number>) {
    super(destination);
  }
 
  protected _next(value: T) {
    let subscription = this.durationSubscription;
    const duration = tryCatch(this.durationSelector)(value);
 
    if (duration === errorObject) {
      this.destination.error(errorObject.e);
    } else {
      this.value = value;
      this.hasValue = true;
      if (subscription) {
        subscription.unsubscribe();
        this.remove(subscription);
      }
      subscription = subscribeToResult(this, duration);
      if (!subscription.isUnsubscribed) {
        this.add(this.durationSubscription = subscription);
      }
    }
  }
 
  protected _complete() {
    this.emitValue();
    this.destination.complete();
  }
 
  notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number): void {
    this.emitValue();
  }
 
  notifyComplete(): void {
    this.emitValue();
  }
 
  emitValue() {
    if (this.hasValue) {
      const value = this.value;
      const subscription = this.durationSubscription;
      if (subscription) {
        this.durationSubscription = null;
        subscription.unsubscribe();
        this.remove(subscription);
      }
      this.value = null;
      this.hasValue = false;
      super._next(value);
    }
  }
}