find.ts 3.8 KB

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