This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/rxjs/_esm2015/internal/operators/single.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-08-11 18:48:02 +00:00
import { Subscriber } from '../Subscriber';
import { EmptyError } from '../util/EmptyError';
export function single(predicate) {
return (source) => source.lift(new SingleOperator(predicate, source));
}
class SingleOperator {
constructor(predicate, source) {
this.predicate = predicate;
this.source = source;
}
call(subscriber, source) {
return source.subscribe(new SingleSubscriber(subscriber, this.predicate, this.source));
}
}
class SingleSubscriber extends Subscriber {
constructor(destination, predicate, source) {
super(destination);
this.predicate = predicate;
this.source = source;
this.seenValue = false;
this.index = 0;
}
applySingleValue(value) {
if (this.seenValue) {
this.destination.error('Sequence contains more than one element');
}
else {
this.seenValue = true;
this.singleValue = value;
}
}
_next(value) {
const index = this.index++;
if (this.predicate) {
this.tryNext(value, index);
}
else {
this.applySingleValue(value);
}
}
tryNext(value, index) {
try {
if (this.predicate(value, index, this.source)) {
this.applySingleValue(value);
}
}
catch (err) {
this.destination.error(err);
}
}
_complete() {
const destination = this.destination;
if (this.index > 0) {
destination.next(this.seenValue ? this.singleValue : undefined);
destination.complete();
}
else {
destination.error(new EmptyError);
}
}
}
//# sourceMappingURL=single.js.map