single.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.single = void 0;
  4. var EmptyError_1 = require("../util/EmptyError");
  5. var SequenceError_1 = require("../util/SequenceError");
  6. var NotFoundError_1 = require("../util/NotFoundError");
  7. var lift_1 = require("../util/lift");
  8. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  9. function single(predicate) {
  10. return lift_1.operate(function (source, subscriber) {
  11. var hasValue = false;
  12. var singleValue;
  13. var seenValue = false;
  14. var index = 0;
  15. source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  16. seenValue = true;
  17. if (!predicate || predicate(value, index++, source)) {
  18. hasValue && subscriber.error(new SequenceError_1.SequenceError('Too many matching values'));
  19. hasValue = true;
  20. singleValue = value;
  21. }
  22. }, function () {
  23. if (hasValue) {
  24. subscriber.next(singleValue);
  25. subscriber.complete();
  26. }
  27. else {
  28. subscriber.error(seenValue ? new NotFoundError_1.NotFoundError('No matching values') : new EmptyError_1.EmptyError());
  29. }
  30. }));
  31. });
  32. }
  33. exports.single = single;
  34. //# sourceMappingURL=single.js.map