work.suroh.tk/node_modules/rxjs/operators/scan.js

121 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-12-02 12:22:45 +00:00
"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');
/* tslint:enable:max-line-length */
/**
* Applies an accumulator function over the source Observable, and returns each
* intermediate result, with an optional seed value.
*
* <span class="informal">It's like {@link reduce}, but emits the current
* accumulation whenever the source emits a value.</span>
*
* <img src="./img/scan.png" width="100%">
*
* Combines together all values emitted on the source, using an accumulator
* function that knows how to join a new source value into the accumulation from
* the past. Is similar to {@link reduce}, but emits the intermediate
* accumulations.
*
* 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.
*
* @example <caption>Count the number of click events</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var ones = clicks.mapTo(1);
* var seed = 0;
* var count = ones.scan((acc, one) => acc + one, seed);
* count.subscribe(x => console.log(x));
*
* @see {@link expand}
* @see {@link mergeScan}
* @see {@link reduce}
*
* @param {function(acc: R, value: T, index: number): R} accumulator
* The accumulator function called on each source value.
* @param {T|R} [seed] The initial accumulation value.
* @return {Observable<R>} An observable of the accumulated values.
* @method scan
* @owner Observable
*/
function scan(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 function scanOperatorFunction(source) {
return source.lift(new ScanOperator(accumulator, seed, hasSeed));
};
}
exports.scan = scan;
var ScanOperator = (function () {
function ScanOperator(accumulator, seed, hasSeed) {
if (hasSeed === void 0) { hasSeed = false; }
this.accumulator = accumulator;
this.seed = seed;
this.hasSeed = hasSeed;
}
ScanOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
};
return ScanOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var ScanSubscriber = (function (_super) {
__extends(ScanSubscriber, _super);
function ScanSubscriber(destination, accumulator, _seed, hasSeed) {
_super.call(this, destination);
this.accumulator = accumulator;
this._seed = _seed;
this.hasSeed = hasSeed;
this.index = 0;
}
Object.defineProperty(ScanSubscriber.prototype, "seed", {
get: function () {
return this._seed;
},
set: function (value) {
this.hasSeed = true;
this._seed = value;
},
enumerable: true,
configurable: true
});
ScanSubscriber.prototype._next = function (value) {
if (!this.hasSeed) {
this.seed = value;
this.destination.next(value);
}
else {
return this._tryNext(value);
}
};
ScanSubscriber.prototype._tryNext = function (value) {
var index = this.index++;
var result;
try {
result = this.accumulator(this.seed, value, index);
}
catch (err) {
this.destination.error(err);
}
this.seed = result;
this.destination.next(result);
};
return ScanSubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=scan.js.map