123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /** 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
|