github-pages-deploy-action/node_modules/rxjs/_esm5/internal/operators/single.js

69 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
/** 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