first.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * Emits only the first value (or the first value that meets some condition)
  13. * emitted by the source Observable.
  14. *
  15. * <span class="informal">Emits only the first value. Or emits only the first
  16. * value that passes some test.</span>
  17. *
  18. * <img src="./img/first.png" width="100%">
  19. *
  20. * If called with no arguments, `first` emits the first value of the source
  21. * Observable, then completes. If called with a `predicate` function, `first`
  22. * emits the first value of the source that matches the specified condition. It
  23. * may also take a `resultSelector` function to produce the output value from
  24. * the input value, and a `defaultValue` to emit in case the source completes
  25. * before it is able to emit a valid value. Throws an error if `defaultValue`
  26. * was not provided and a matching element is not found.
  27. *
  28. * @example <caption>Emit only the first click that happens on the DOM</caption>
  29. * var clicks = Rx.Observable.fromEvent(document, 'click');
  30. * var result = clicks.first();
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * @example <caption>Emits the first click that happens on a DIV</caption>
  34. * var clicks = Rx.Observable.fromEvent(document, 'click');
  35. * var result = clicks.first(ev => ev.target.tagName === 'DIV');
  36. * result.subscribe(x => console.log(x));
  37. *
  38. * @see {@link filter}
  39. * @see {@link find}
  40. * @see {@link take}
  41. *
  42. * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
  43. * callback if the Observable completes before any `next` notification was sent.
  44. *
  45. * @param {function(value: T, index: number, source: Observable<T>): boolean} [predicate]
  46. * An optional function called with each item to test for condition matching.
  47. * @param {function(value: T, index: number): R} [resultSelector] A function to
  48. * produce the value on the output Observable based on the values
  49. * and the indices of the source Observable. The arguments passed to this
  50. * function are:
  51. * - `value`: the value that was emitted on the source.
  52. * - `index`: the "index" of the value from the source.
  53. * @param {R} [defaultValue] The default value emitted in case no valid value
  54. * was found on the source.
  55. * @return {Observable<T|R>} An Observable of the first item that matches the
  56. * condition.
  57. * @method first
  58. * @owner Observable
  59. */
  60. export function first(predicate, resultSelector, defaultValue) {
  61. return function (source) { return source.lift(new FirstOperator(predicate, resultSelector, defaultValue, source)); };
  62. }
  63. var FirstOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  64. function FirstOperator(predicate, resultSelector, defaultValue, source) {
  65. this.predicate = predicate;
  66. this.resultSelector = resultSelector;
  67. this.defaultValue = defaultValue;
  68. this.source = source;
  69. }
  70. FirstOperator.prototype.call = function (observer, source) {
  71. return source.subscribe(new FirstSubscriber(observer, this.predicate, this.resultSelector, this.defaultValue, this.source));
  72. };
  73. return FirstOperator;
  74. }());
  75. /**
  76. * We need this JSDoc comment for affecting ESDoc.
  77. * @ignore
  78. * @extends {Ignored}
  79. */
  80. var FirstSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  81. __extends(FirstSubscriber, _super);
  82. function FirstSubscriber(destination, predicate, resultSelector, defaultValue, source) {
  83. _super.call(this, destination);
  84. this.predicate = predicate;
  85. this.resultSelector = resultSelector;
  86. this.defaultValue = defaultValue;
  87. this.source = source;
  88. this.index = 0;
  89. this.hasCompleted = false;
  90. this._emitted = false;
  91. }
  92. FirstSubscriber.prototype._next = function (value) {
  93. var index = this.index++;
  94. if (this.predicate) {
  95. this._tryPredicate(value, index);
  96. }
  97. else {
  98. this._emit(value, index);
  99. }
  100. };
  101. FirstSubscriber.prototype._tryPredicate = function (value, index) {
  102. var result;
  103. try {
  104. result = this.predicate(value, index, this.source);
  105. }
  106. catch (err) {
  107. this.destination.error(err);
  108. return;
  109. }
  110. if (result) {
  111. this._emit(value, index);
  112. }
  113. };
  114. FirstSubscriber.prototype._emit = function (value, index) {
  115. if (this.resultSelector) {
  116. this._tryResultSelector(value, index);
  117. return;
  118. }
  119. this._emitFinal(value);
  120. };
  121. FirstSubscriber.prototype._tryResultSelector = function (value, index) {
  122. var result;
  123. try {
  124. result = this.resultSelector(value, index);
  125. }
  126. catch (err) {
  127. this.destination.error(err);
  128. return;
  129. }
  130. this._emitFinal(result);
  131. };
  132. FirstSubscriber.prototype._emitFinal = function (value) {
  133. var destination = this.destination;
  134. if (!this._emitted) {
  135. this._emitted = true;
  136. destination.next(value);
  137. destination.complete();
  138. this.hasCompleted = true;
  139. }
  140. };
  141. FirstSubscriber.prototype._complete = function () {
  142. var destination = this.destination;
  143. if (!this.hasCompleted && typeof this.defaultValue !== 'undefined') {
  144. destination.next(this.defaultValue);
  145. destination.complete();
  146. }
  147. else if (!this.hasCompleted) {
  148. destination.error(new EmptyError);
  149. }
  150. };
  151. return FirstSubscriber;
  152. }(Subscriber));
  153. //# sourceMappingURL=first.js.map