Code coverage report for src/operator/distinctUntilChanged.ts

Statements: 100% (33 / 33)      Branches: 100% (8 / 8)      Functions: 100% (8 / 8)      Lines: 100% (30 / 30)      Ignored: none     

All files » src/operator/ » distinctUntilChanged.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  1 1 1   1 34     1 34     1 34   1   1   34   1 34 34 21       1 26     1 107   107 83 83 2 2     24     105 62 62     1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
export function distinctUntilChanged<T>(compare?: (x: T, y: T) => boolean) {
  return this.lift(new DistinctUntilChangedOperator(compare));
}
 
class DistinctUntilChangedOperator<T, R> implements Operator<T, R> {
  constructor(private compare: (x: T, y: T) => boolean) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new DistinctUntilChangedSubscriber(subscriber, this.compare);
  }
}
 
class DistinctUntilChangedSubscriber<T> extends Subscriber<T> {
  private value: T;
  private hasValue: boolean = false;
 
  constructor(destination: Subscriber<T>, compare: (x: T, y: T) => boolean) {
    super(destination);
    if (typeof compare === 'function') {
      this.compare = compare;
    }
  }
 
  private compare(x: T, y: T): boolean {
    return x === y;
  }
 
  _next(value: T): void {
    let result: any = false;
 
    if (this.hasValue) {
      result = tryCatch(this.compare)(this.value, value);
      if (result === errorObject) {
        this.destination.error(errorObject.e);
        return;
      }
    } else {
      this.hasValue = true;
    }
 
    if (Boolean(result) === false) {
      this.value = value;
      this.destination.next(value);
    }
  }
}