es6/operator/reduce.js
import { Subscriber } from '../Subscriber';
/* tslint:disable:max-line-length */
export function reduce(accumulator, seed) {
var hasSeed = false;
// providing a seed of `undefined` *should* be valid and trigger
// hasSeed! so don't use `seed !== undefined` checks!
// For this reason, we have to check it here at the original call site
// otherwise inside Operator/Subscriber we won't know if `undefined`
// means they didn't provide anything or if they literally provided `undefined`
if (arguments.length >= 2) {
hasSeed = true;
}
return this.lift(new ReduceOperator(accumulator, seed, hasSeed));
}
export var ReduceOperator = (function () {
function ReduceOperator(accumulator, seed, hasSeed) {
if (hasSeed === void 0) { hasSeed = false; }
this.accumulator = accumulator;
this.seed = seed;
this.hasSeed = hasSeed;
}
ReduceOperator.prototype.call = function (subscriber, source) {
return source._subscribe(new ReduceSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
};
return ReduceOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export var ReduceSubscriber = (function (_super) {
__extends(ReduceSubscriber, _super);
function ReduceSubscriber(destination, accumulator, seed, hasSeed) {
_super.call(this, destination);
this.accumulator = accumulator;
this.hasSeed = hasSeed;
this.hasValue = false;
this.acc = seed;
}
ReduceSubscriber.prototype._next = function (value) {
if (this.hasValue || (this.hasValue = this.hasSeed)) {
this._tryReduce(value);
}
else {
this.acc = value;
this.hasValue = true;
}
};
ReduceSubscriber.prototype._tryReduce = function (value) {
var result;
try {
result = this.accumulator(this.acc, value);
}
catch (err) {
this.destination.error(err);
return;
}
this.acc = result;
};
ReduceSubscriber.prototype._complete = function () {
if (this.hasValue || this.hasSeed) {
this.destination.next(this.acc);
}
this.destination.complete();
};
return ReduceSubscriber;
}(Subscriber));
//# sourceMappingURL=reduce.js.map