1 | /** PURE_IMPORTS_START tslib,_Subscriber,_util_EmptyError PURE_IMPORTS_END */
|
---|
2 | import * as tslib_1 from "tslib";
|
---|
3 | import { Subscriber } from '../Subscriber';
|
---|
4 | import { EmptyError } from '../util/EmptyError';
|
---|
5 | export function single(predicate) {
|
---|
6 | return function (source) { return source.lift(new SingleOperator(predicate, source)); };
|
---|
7 | }
|
---|
8 | var SingleOperator = /*@__PURE__*/ (function () {
|
---|
9 | function SingleOperator(predicate, source) {
|
---|
10 | this.predicate = predicate;
|
---|
11 | this.source = source;
|
---|
12 | }
|
---|
13 | SingleOperator.prototype.call = function (subscriber, source) {
|
---|
14 | return source.subscribe(new SingleSubscriber(subscriber, this.predicate, this.source));
|
---|
15 | };
|
---|
16 | return SingleOperator;
|
---|
17 | }());
|
---|
18 | var SingleSubscriber = /*@__PURE__*/ (function (_super) {
|
---|
19 | tslib_1.__extends(SingleSubscriber, _super);
|
---|
20 | function SingleSubscriber(destination, predicate, source) {
|
---|
21 | var _this = _super.call(this, destination) || this;
|
---|
22 | _this.predicate = predicate;
|
---|
23 | _this.source = source;
|
---|
24 | _this.seenValue = false;
|
---|
25 | _this.index = 0;
|
---|
26 | return _this;
|
---|
27 | }
|
---|
28 | SingleSubscriber.prototype.applySingleValue = function (value) {
|
---|
29 | if (this.seenValue) {
|
---|
30 | this.destination.error('Sequence contains more than one element');
|
---|
31 | }
|
---|
32 | else {
|
---|
33 | this.seenValue = true;
|
---|
34 | this.singleValue = value;
|
---|
35 | }
|
---|
36 | };
|
---|
37 | SingleSubscriber.prototype._next = function (value) {
|
---|
38 | var index = this.index++;
|
---|
39 | if (this.predicate) {
|
---|
40 | this.tryNext(value, index);
|
---|
41 | }
|
---|
42 | else {
|
---|
43 | this.applySingleValue(value);
|
---|
44 | }
|
---|
45 | };
|
---|
46 | SingleSubscriber.prototype.tryNext = function (value, index) {
|
---|
47 | try {
|
---|
48 | if (this.predicate(value, index, this.source)) {
|
---|
49 | this.applySingleValue(value);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | catch (err) {
|
---|
53 | this.destination.error(err);
|
---|
54 | }
|
---|
55 | };
|
---|
56 | SingleSubscriber.prototype._complete = function () {
|
---|
57 | var destination = this.destination;
|
---|
58 | if (this.index > 0) {
|
---|
59 | destination.next(this.seenValue ? this.singleValue : undefined);
|
---|
60 | destination.complete();
|
---|
61 | }
|
---|
62 | else {
|
---|
63 | destination.error(new EmptyError);
|
---|
64 | }
|
---|
65 | };
|
---|
66 | return SingleSubscriber;
|
---|
67 | }(Subscriber));
|
---|
68 | //# sourceMappingURL=single.js.map
|
---|