76 lines
3.2 KiB
JavaScript
76 lines
3.2 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 OuterSubscriber_1 = require('../OuterSubscriber');
|
|
var subscribeToResult_1 = require('../util/subscribeToResult');
|
|
/**
|
|
* Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items.
|
|
* If a comparator function is provided, then it will be called for each item to test for whether or not that value should be emitted.
|
|
* If a comparator function is not provided, an equality check is used by default.
|
|
* As the internal HashSet of this operator grows larger and larger, care should be taken in the domain of inputs this operator may see.
|
|
* An optional paramter is also provided such that an Observable can be provided to queue the internal HashSet to flush the values it holds.
|
|
* @param {function} [compare] optional comparison function called to test if an item is distinct from previous items in the source.
|
|
* @param {Observable} [flushes] optional Observable for flushing the internal HashSet of the operator.
|
|
* @returns {Observable} an Observable that emits items from the source Observable with distinct values.
|
|
*/
|
|
function distinct(compare, flushes) {
|
|
return this.lift(new DistinctOperator(compare, flushes));
|
|
}
|
|
exports.distinct = distinct;
|
|
var DistinctOperator = (function () {
|
|
function DistinctOperator(compare, flushes) {
|
|
this.compare = compare;
|
|
this.flushes = flushes;
|
|
}
|
|
DistinctOperator.prototype.call = function (subscriber) {
|
|
return new DistinctSubscriber(subscriber, this.compare, this.flushes);
|
|
};
|
|
return DistinctOperator;
|
|
}());
|
|
var DistinctSubscriber = (function (_super) {
|
|
__extends(DistinctSubscriber, _super);
|
|
function DistinctSubscriber(destination, compare, flushes) {
|
|
_super.call(this, destination);
|
|
this.values = [];
|
|
if (typeof compare === 'function') {
|
|
this.compare = compare;
|
|
}
|
|
if (flushes) {
|
|
this.add(subscribeToResult_1.subscribeToResult(this, flushes));
|
|
}
|
|
}
|
|
DistinctSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
|
this.values.length = 0;
|
|
};
|
|
DistinctSubscriber.prototype.notifyError = function (error, innerSub) {
|
|
this._error(error);
|
|
};
|
|
DistinctSubscriber.prototype._next = function (value) {
|
|
var found = false;
|
|
var values = this.values;
|
|
var len = values.length;
|
|
try {
|
|
for (var i = 0; i < len; i++) {
|
|
if (this.compare(values[i], value)) {
|
|
found = true;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
catch (err) {
|
|
this.destination.error(err);
|
|
return;
|
|
}
|
|
this.values.push(value);
|
|
this.destination.next(value);
|
|
};
|
|
DistinctSubscriber.prototype.compare = function (x, y) {
|
|
return x === y;
|
|
};
|
|
return DistinctSubscriber;
|
|
}(OuterSubscriber_1.OuterSubscriber));
|
|
exports.DistinctSubscriber = DistinctSubscriber;
|
|
//# sourceMappingURL=distinct.js.map
|