wishthis/node_modules/rxjs/_esm5/internal/operators/single.js
2022-05-29 11:24:36 +02:00

68 lines
2.3 KiB
JavaScript

/** PURE_IMPORTS_START tslib,_Subscriber,_util_EmptyError PURE_IMPORTS_END */
import * as tslib_1 from "tslib";
import { Subscriber } from '../Subscriber';
import { EmptyError } from '../util/EmptyError';
export function single(predicate) {
return function (source) { return source.lift(new SingleOperator(predicate, source)); };
}
var SingleOperator = /*@__PURE__*/ (function () {
function SingleOperator(predicate, source) {
this.predicate = predicate;
this.source = source;
}
SingleOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new SingleSubscriber(subscriber, this.predicate, this.source));
};
return SingleOperator;
}());
var SingleSubscriber = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(SingleSubscriber, _super);
function SingleSubscriber(destination, predicate, source) {
var _this = _super.call(this, destination) || this;
_this.predicate = predicate;
_this.source = source;
_this.seenValue = false;
_this.index = 0;
return _this;
}
SingleSubscriber.prototype.applySingleValue = function (value) {
if (this.seenValue) {
this.destination.error('Sequence contains more than one element');
}
else {
this.seenValue = true;
this.singleValue = value;
}
};
SingleSubscriber.prototype._next = function (value) {
var index = this.index++;
if (this.predicate) {
this.tryNext(value, index);
}
else {
this.applySingleValue(value);
}
};
SingleSubscriber.prototype.tryNext = function (value, index) {
try {
if (this.predicate(value, index, this.source)) {
this.applySingleValue(value);
}
}
catch (err) {
this.destination.error(err);
}
};
SingleSubscriber.prototype._complete = function () {
var destination = this.destination;
if (this.index > 0) {
destination.next(this.seenValue ? this.singleValue : undefined);
destination.complete();
}
else {
destination.error(new EmptyError);
}
};
return SingleSubscriber;
}(Subscriber));
//# sourceMappingURL=single.js.map