find.js 3.0 KB

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