single.js 3.4KB

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