1 import { Subscriber } from '../Subscriber';
2 import { EmptyError } from '../util/EmptyError';
3 export function single(predicate) {
4 return (source) => source.lift(new SingleOperator(predicate, source));
7 constructor(predicate, source) {
8 this.predicate = predicate;
11 call(subscriber, source) {
12 return source.subscribe(new SingleSubscriber(subscriber, this.predicate, this.source));
15 class SingleSubscriber extends Subscriber {
16 constructor(destination, predicate, source) {
18 this.predicate = predicate;
20 this.seenValue = false;
23 applySingleValue(value) {
25 this.destination.error('Sequence contains more than one element');
28 this.seenValue = true;
29 this.singleValue = value;
33 const index = this.index++;
35 this.tryNext(value, index);
38 this.applySingleValue(value);
41 tryNext(value, index) {
43 if (this.predicate(value, index, this.source)) {
44 this.applySingleValue(value);
48 this.destination.error(err);
52 const destination = this.destination;
54 destination.next(this.seenValue ? this.singleValue : undefined);
55 destination.complete();
58 destination.error(new EmptyError);
62 //# sourceMappingURL=single.js.map