123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Subscriber_1 = require('../Subscriber');
  8. var EmptyError_1 = require('../util/EmptyError');
  9. /**
  10. * Returns an Observable that emits the single item emitted by the source Observable that matches a specified
  11. * predicate, if that Observable emits one such item. If the source Observable emits more than one such item or no
  12. * such items, notify of an IllegalArgumentException or NoSuchElementException respectively.
  13. *
  14. * <img src="./img/single.png" width="100%">
  15. *
  16. * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
  17. * callback if the Observable completes before any `next` notification was sent.
  18. * @param {Function} predicate - A predicate function to evaluate items emitted by the source Observable.
  19. * @return {Observable<T>} An Observable that emits the single item emitted by the source Observable that matches
  20. * the predicate.
  21. .
  22. * @method single
  23. * @owner Observable
  24. */
  25. function single(predicate) {
  26. return function (source) { return source.lift(new SingleOperator(predicate, source)); };
  27. }
  28. exports.single = single;
  29. var SingleOperator = (function () {
  30. function SingleOperator(predicate, source) {
  31. this.predicate = predicate;
  32. this.source = source;
  33. }
  34. SingleOperator.prototype.call = function (subscriber, source) {
  35. return source.subscribe(new SingleSubscriber(subscriber, this.predicate, this.source));
  36. };
  37. return SingleOperator;
  38. }());
  39. /**
  40. * We need this JSDoc comment for affecting ESDoc.
  41. * @ignore
  42. * @extends {Ignored}
  43. */
  44. var SingleSubscriber = (function (_super) {
  45. __extends(SingleSubscriber, _super);
  46. function SingleSubscriber(destination, predicate, source) {
  47. _super.call(this, destination);
  48. this.predicate = predicate;
  49. this.source = source;
  50. this.seenValue = false;
  51. this.index = 0;
  52. }
  53. SingleSubscriber.prototype.applySingleValue = function (value) {
  54. if (this.seenValue) {
  55. this.destination.error('Sequence contains more than one element');
  56. }
  57. else {
  58. this.seenValue = true;
  59. this.singleValue = value;
  60. }
  61. };
  62. SingleSubscriber.prototype._next = function (value) {
  63. var index = this.index++;
  64. if (this.predicate) {
  65. this.tryNext(value, index);
  66. }
  67. else {
  68. this.applySingleValue(value);
  69. }
  70. };
  71. SingleSubscriber.prototype.tryNext = function (value, index) {
  72. try {
  73. if (this.predicate(value, index, this.source)) {
  74. this.applySingleValue(value);
  75. }
  76. }
  77. catch (err) {
  78. this.destination.error(err);
  79. }
  80. };
  81. SingleSubscriber.prototype._complete = function () {
  82. var destination = this.destination;
  83. if (this.index > 0) {
  84. destination.next(this.seenValue ? this.singleValue : undefined);
  85. destination.complete();
  86. }
  87. else {
  88. destination.error(new EmptyError_1.EmptyError);
  89. }
  90. };
  91. return SingleSubscriber;
  92. }(Subscriber_1.Subscriber));
  93. //# sourceMappingURL=single.js.map