find.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /** PURE_IMPORTS_START .._Subscriber 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. /**
  11. * Emits only the first value emitted by the source Observable that meets some
  12. * condition.
  13. *
  14. * <span class="informal">Finds the first value that passes some test and emits
  15. * that.</span>
  16. *
  17. * <img src="./img/find.png" width="100%">
  18. *
  19. * `find` searches for the first item in the source Observable that matches the
  20. * specified condition embodied by the `predicate`, and returns the first
  21. * occurrence in the source. Unlike {@link first}, the `predicate` is required
  22. * in `find`, and does not emit an error if a valid value is not found.
  23. *
  24. * @example <caption>Find and emit the first click that happens on a DIV element</caption>
  25. * var clicks = Rx.Observable.fromEvent(document, 'click');
  26. * var result = clicks.find(ev => ev.target.tagName === 'DIV');
  27. * result.subscribe(x => console.log(x));
  28. *
  29. * @see {@link filter}
  30. * @see {@link first}
  31. * @see {@link findIndex}
  32. * @see {@link take}
  33. *
  34. * @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
  35. * A function called with each item to test for condition matching.
  36. * @param {any} [thisArg] An optional argument to determine the value of `this`
  37. * in the `predicate` function.
  38. * @return {Observable<T>} An Observable of the first item that matches the
  39. * condition.
  40. * @method find
  41. * @owner Observable
  42. */
  43. export function find(predicate, thisArg) {
  44. if (typeof predicate !== 'function') {
  45. throw new TypeError('predicate is not a function');
  46. }
  47. return function (source) { return source.lift(new FindValueOperator(predicate, source, false, thisArg)); };
  48. }
  49. export var FindValueOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  50. function FindValueOperator(predicate, source, yieldIndex, thisArg) {
  51. this.predicate = predicate;
  52. this.source = source;
  53. this.yieldIndex = yieldIndex;
  54. this.thisArg = thisArg;
  55. }
  56. FindValueOperator.prototype.call = function (observer, source) {
  57. return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
  58. };
  59. return FindValueOperator;
  60. }());
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. export var FindValueSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  67. __extends(FindValueSubscriber, _super);
  68. function FindValueSubscriber(destination, predicate, source, yieldIndex, thisArg) {
  69. _super.call(this, destination);
  70. this.predicate = predicate;
  71. this.source = source;
  72. this.yieldIndex = yieldIndex;
  73. this.thisArg = thisArg;
  74. this.index = 0;
  75. }
  76. FindValueSubscriber.prototype.notifyComplete = function (value) {
  77. var destination = this.destination;
  78. destination.next(value);
  79. destination.complete();
  80. };
  81. FindValueSubscriber.prototype._next = function (value) {
  82. var _a = this, predicate = _a.predicate, thisArg = _a.thisArg;
  83. var index = this.index++;
  84. try {
  85. var result = predicate.call(thisArg || this, value, index, this.source);
  86. if (result) {
  87. this.notifyComplete(this.yieldIndex ? index : value);
  88. }
  89. }
  90. catch (err) {
  91. this.destination.error(err);
  92. }
  93. };
  94. FindValueSubscriber.prototype._complete = function () {
  95. this.notifyComplete(this.yieldIndex ? -1 : undefined);
  96. };
  97. return FindValueSubscriber;
  98. }(Subscriber));
  99. //# sourceMappingURL=find.js.map