Code coverage report for src/operator/scan.ts

Statements: 100% (36 / 36)      Branches: 100% (4 / 4)      Functions: 100% (9 / 9)      Lines: 100% (31 / 31)      Ignored: none     

All files » src/operator/ » scan.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    1 1 1   1 8     1 8     1 8   1   1     1 35       26 26     8   8 8 8 8 8     1 19 1 1   18 18 1   17 17       1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
export function scan<T, R>(accumulator: (acc: R, x: T) => R, seed?: T | R): Observable<R> {
  return this.lift(new ScanOperator(accumulator, seed));
}
 
class ScanOperator<T, R> implements Operator<T, R> {
  constructor(private accumulator: (acc: R, x: T) => R, private seed?: T | R) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new ScanSubscriber(subscriber, this.accumulator, this.seed);
  }
}
 
class ScanSubscriber<T, R> extends Subscriber<T> {
  private _seed: T | R;
 
  get seed(): T | R {
    return this._seed;
  }
 
  set seed(value: T | R) {
    this.accumulatorSet = true;
    this._seed = value;
  }
 
  private accumulatorSet: boolean = false;
 
  constructor(destination: Subscriber<T>, private accumulator: (acc: R, x: T) => R, seed?: T|R) {
    super(destination);
    this.seed = seed;
    this.accumulator = accumulator;
    this.accumulatorSet = typeof seed !== 'undefined';
  }
 
  _next(value: T): void {
    if (!this.accumulatorSet) {
      this.seed = value;
      this.destination.next(value);
    } else {
      const result = tryCatch(this.accumulator).call(this, this.seed, value);
      if (result === errorObject) {
        this.destination.error(errorObject.e);
      } else {
        this.seed = result;
        this.destination.next(this.seed);
      }
    }
  }
}