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 57 58 59 60 61 62 63 64 65 66 67    1 1 1                         1 10     1 10     1 10   1   1     1 49       35 35     10   10 10 10 10 10     1 26 1 1   25 25 1   24 24       1  
import {Operator} from '../Operator';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
/**
 * Returns an Observable that applies a specified accumulator function to each item emitted by the source Observable.
 * If a seed value is specified, then that value will be used as the initial value for the accumulator.
 * If no seed value is specified, the first item of the source is used as the seed.
 * @param {function} accumulator The accumulator function called on each item.
 *
 * <img src="./img/scan.png" width="100%">
 *
 * @param {any} [seed] The initial accumulator value.
 * @returns {Obervable} An observable of the accumulated values.
 */
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';
  }
 
  protected _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);
      }
    }
  }
}