Code coverage report for src/operator/reduce-support.ts

Statements: 100% (33 / 33)      Branches: 100% (10 / 10)      Functions: 100% (7 / 7)      Lines: 100% (28 / 28)      Ignored: none     

All files » src/operator/ » reduce-support.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  1 1 1   1   60     1 60   1   1       60     1 60 60 60 60     1 20101 20066 20066 4   20062     35 35       1 30 23   30   1  
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
export class ReduceOperator<T, R> implements Operator<T, R> {
 
  constructor(private project: (acc: R, x: T) => R, private seed?: R) {
  }
 
  call(subscriber: Subscriber<T>): Subscriber<T> {
    return new ReduceSubscriber(subscriber, this.project, this.seed);
  }
}
 
export class ReduceSubscriber<T, R> extends Subscriber<T> {
 
  acc: R;
  hasSeed: boolean;
  hasValue: boolean = false;
  project: (acc: R, x: T) => R;
 
  constructor(destination: Subscriber<T>, project: (acc: R, x: T) => R, seed?: R) {
    super(destination);
    this.acc = seed;
    this.project = project;
    this.hasSeed = typeof seed !== 'undefined';
  }
 
  _next(x) {
    if (this.hasValue || (this.hasValue = this.hasSeed)) {
      const result = tryCatch(this.project).call(this, this.acc, x);
      if (result === errorObject) {
        this.destination.error(errorObject.e);
      } else {
        this.acc = result;
      }
    } else {
      this.acc = x;
      this.hasValue = true;
    }
  }
 
  _complete() {
    if (this.hasValue || this.hasSeed) {
      this.destination.next(this.acc);
    }
    this.destination.complete();
  }
}