3d1bc3cfdf71b5cbea63b8e4c7cf9e340ceafdee1a3ce4cd794a058e18fad35dae8fe70292b1c206dd847a4ca94ec7981a6a89318494252d6369407b4a90d8 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Observable } from '../Observable';
  2. import { Subscriber } from '../Subscriber';
  3. import { OperatorFunction, TruthyTypesOf } from '../types';
  4. import { operate } from '../util/lift';
  5. import { createOperatorSubscriber } from './OperatorSubscriber';
  6. export function find<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
  7. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  8. export function find<T, S extends T, A>(
  9. predicate: (this: A, value: T, index: number, source: Observable<T>) => value is S,
  10. thisArg: A
  11. ): OperatorFunction<T, S | undefined>;
  12. export function find<T, S extends T>(
  13. predicate: (value: T, index: number, source: Observable<T>) => value is S
  14. ): OperatorFunction<T, S | undefined>;
  15. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  16. export function find<T, A>(
  17. predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
  18. thisArg: A
  19. ): OperatorFunction<T, T | undefined>;
  20. export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, T | undefined>;
  21. /**
  22. * Emits only the first value emitted by the source Observable that meets some
  23. * condition.
  24. *
  25. * <span class="informal">Finds the first value that passes some test and emits
  26. * that.</span>
  27. *
  28. * ![](find.png)
  29. *
  30. * `find` searches for the first item in the source Observable that matches the
  31. * specified condition embodied by the `predicate`, and returns the first
  32. * occurrence in the source. Unlike {@link first}, the `predicate` is required
  33. * in `find`, and does not emit an error if a valid value is not found
  34. * (emits `undefined` instead).
  35. *
  36. * ## Example
  37. *
  38. * Find and emit the first click that happens on a DIV element
  39. *
  40. * ```ts
  41. * import { fromEvent, find } from 'rxjs';
  42. *
  43. * const div = document.createElement('div');
  44. * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
  45. * document.body.appendChild(div);
  46. *
  47. * const clicks = fromEvent(document, 'click');
  48. * const result = clicks.pipe(find(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
  49. * result.subscribe(x => console.log(x));
  50. * ```
  51. *
  52. * @see {@link filter}
  53. * @see {@link first}
  54. * @see {@link findIndex}
  55. * @see {@link take}
  56. *
  57. * @param predicate A function called with each item to test for condition matching.
  58. * @param thisArg An optional argument to determine the value of `this` in the
  59. * `predicate` function.
  60. * @return A function that returns an Observable that emits the first item that
  61. * matches the condition.
  62. */
  63. export function find<T>(
  64. predicate: (value: T, index: number, source: Observable<T>) => boolean,
  65. thisArg?: any
  66. ): OperatorFunction<T, T | undefined> {
  67. return operate(createFind(predicate, thisArg, 'value'));
  68. }
  69. export function createFind<T>(
  70. predicate: (value: T, index: number, source: Observable<T>) => boolean,
  71. thisArg: any,
  72. emit: 'value' | 'index'
  73. ) {
  74. const findIndex = emit === 'index';
  75. return (source: Observable<T>, subscriber: Subscriber<any>) => {
  76. let index = 0;
  77. source.subscribe(
  78. createOperatorSubscriber(
  79. subscriber,
  80. (value) => {
  81. const i = index++;
  82. if (predicate.call(thisArg, value, i, source)) {
  83. subscriber.next(findIndex ? i : value);
  84. subscriber.complete();
  85. }
  86. },
  87. () => {
  88. subscriber.next(findIndex ? -1 : undefined);
  89. subscriber.complete();
  90. }
  91. )
  92. );
  93. };
  94. }