76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
"use strict";
|
|
var __extends = (this && this.__extends) || function (d, b) {
|
|
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
function __() { this.constructor = d; }
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
};
|
|
var Subscriber_1 = require('../Subscriber');
|
|
/**
|
|
* 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.
|
|
*/
|
|
function scan(accumulator, seed) {
|
|
return this.lift(new ScanOperator(accumulator, seed));
|
|
}
|
|
exports.scan = scan;
|
|
var ScanOperator = (function () {
|
|
function ScanOperator(accumulator, seed) {
|
|
this.accumulator = accumulator;
|
|
this.seed = seed;
|
|
}
|
|
ScanOperator.prototype.call = function (subscriber) {
|
|
return new ScanSubscriber(subscriber, this.accumulator, this.seed);
|
|
};
|
|
return ScanOperator;
|
|
}());
|
|
var ScanSubscriber = (function (_super) {
|
|
__extends(ScanSubscriber, _super);
|
|
function ScanSubscriber(destination, accumulator, seed) {
|
|
_super.call(this, destination);
|
|
this.accumulator = accumulator;
|
|
this.accumulatorSet = false;
|
|
this.seed = seed;
|
|
this.accumulator = accumulator;
|
|
this.accumulatorSet = typeof seed !== 'undefined';
|
|
}
|
|
Object.defineProperty(ScanSubscriber.prototype, "seed", {
|
|
get: function () {
|
|
return this._seed;
|
|
},
|
|
set: function (value) {
|
|
this.accumulatorSet = true;
|
|
this._seed = value;
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
ScanSubscriber.prototype._next = function (value) {
|
|
if (!this.accumulatorSet) {
|
|
this.seed = value;
|
|
this.destination.next(value);
|
|
}
|
|
else {
|
|
return this._tryNext(value);
|
|
}
|
|
};
|
|
ScanSubscriber.prototype._tryNext = function (value) {
|
|
var result;
|
|
try {
|
|
result = this.accumulator(this.seed, value);
|
|
}
|
|
catch (err) {
|
|
this.destination.error(err);
|
|
}
|
|
this.seed = result;
|
|
this.destination.next(result);
|
|
};
|
|
return ScanSubscriber;
|
|
}(Subscriber_1.Subscriber));
|
|
//# sourceMappingURL=scan.js.map
|