diff --git a/src/operators/scan.ts b/src/operators/scan.ts index 41dd3d18f5..feba360ce7 100644 --- a/src/operators/scan.ts +++ b/src/operators/scan.ts @@ -6,7 +6,7 @@ import tryCatch from '../util/tryCatch'; import {errorObject} from '../util/errorObject'; export default function scan(project: (acc: R, x: T) => R, acc?: R) { - return this.lift(new ScanOperator(project)); + return this.lift(new ScanOperator(project, acc)); } class ScanOperator implements Operator { @@ -25,36 +25,40 @@ class ScanOperator implements Operator { } class ScanSubscriber extends Subscriber { + private _acc: R; + + get acc(): R { + return this._acc; + } + + set acc(value: R) { + this.accumulatorSet = true; + this._acc = value; + } + + accumulatorSet: boolean = false; - acc: R; - hasSeed: boolean; - hasValue: boolean = false; project: (acc: R, x: T) => R; constructor(destination: Subscriber, project: (acc: R, x: T) => R, acc?: R) { super(destination); this.acc = acc; this.project = project; - this.hasSeed = typeof acc !== 'undefined'; + this.accumulatorSet = typeof acc !== 'undefined'; } _next(x) { - if (this.hasValue || (this.hasValue = this.hasSeed)) { + if (!this.accumulatorSet) { + this.acc = x; + this.destination.next(x); + } else { const result = tryCatch(this.project).call(this, this.acc, x); if (result === errorObject) { this.destination.error(errorObject.e); } else { - this.destination.next(this.acc = result); + this.acc = result; + this.destination.next(this.acc); } - } else { - return this.destination.next((this.hasValue = true) && (this.acc = x)); - } - } - - _complete() { - if (!this.hasValue && this.hasSeed) { - this.destination.next(this.acc); } - this.destination.complete(); } }