Code coverage report for src/operator/reduce.ts

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

All files » src/operator/ » reduce.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    1 1 1                                   1 16     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 {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
 
/**
 * Returns an Observable that applies a specified accumulator function to the first item emitted by a source Observable,
 * then feeds the result of that function along with the second item emitted by the source Observable into the same
 * function, and so on until all items have been emitted by the source Observable, and emits the final result from
 * the final call to your function as its sole item.
 * This technique, which is called "reduce" here, is sometimes called "aggregate," "fold," "accumulate," "compress," or
 * "inject" in other programming contexts.
 *
 * <img src="./img/reduce.png" width="100%">
 *
 * @param {initialValue} the initial (seed) accumulator value
 * @param {accumulator} an accumulator function to be invoked on each item emitted by the source Observable, the
 * result of which will be used in the next accumulator call.
 * @returns {Observable} an Observable that emits a single item that is the result of accumulating the output from the
 * items emitted by the source Observable.
 */
export function reduce<T, R>(project: (acc: R, value: T) => R, seed?: R): Observable<R> {
  return this.lift(new ReduceOperator(project, seed));
}
 
export class ReduceOperator<T, R> implements Operator<T, R> {
 
  constructor(private project: (acc: R, value: 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: T | R;
  hasSeed: boolean;
  hasValue: boolean = false;
  project: (acc: R, value: T) => R;
 
  constructor(destination: Subscriber<T>, project: (acc: R, value: T) => R, seed?: R) {
    super(destination);
    this.acc = seed;
    this.project = project;
    this.hasSeed = typeof seed !== 'undefined';
  }
 
  protected _next(x: T) {
    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;
    }
  }
 
  protected _complete() {
    if (this.hasValue || this.hasSeed) {
      this.destination.next(this.acc);
    }
    this.destination.complete();
  }
}