6befb68017efe3f10b23b0c5b4fcd97d570e0293b9a7bdf8f73d9f441a43f296959b7eeae4eb601fe5d0d087c072438d6d8e32d7f75a6ed2a3fcb8d0fc068f 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Observable } from '../Observable';
  2. import { EmptyError } from '../util/EmptyError';
  3. import { OperatorFunction, TruthyTypesOf } from '../types';
  4. import { filter } from './filter';
  5. import { take } from './take';
  6. import { defaultIfEmpty } from './defaultIfEmpty';
  7. import { throwIfEmpty } from './throwIfEmpty';
  8. import { identity } from '../util/identity';
  9. export function first<T, D = T>(predicate?: null, defaultValue?: D): OperatorFunction<T, T | D>;
  10. export function first<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
  11. export function first<T, D>(predicate: BooleanConstructor, defaultValue: D): OperatorFunction<T, TruthyTypesOf<T> | D>;
  12. export function first<T, S extends T>(
  13. predicate: (value: T, index: number, source: Observable<T>) => value is S,
  14. defaultValue?: S
  15. ): OperatorFunction<T, S>;
  16. export function first<T, S extends T, D>(
  17. predicate: (value: T, index: number, source: Observable<T>) => value is S,
  18. defaultValue: D
  19. ): OperatorFunction<T, S | D>;
  20. export function first<T, D = T>(
  21. predicate: (value: T, index: number, source: Observable<T>) => boolean,
  22. defaultValue?: D
  23. ): OperatorFunction<T, T | D>;
  24. /**
  25. * Emits only the first value (or the first value that meets some condition)
  26. * emitted by the source Observable.
  27. *
  28. * <span class="informal">Emits only the first value. Or emits only the first
  29. * value that passes some test.</span>
  30. *
  31. * ![](first.png)
  32. *
  33. * If called with no arguments, `first` emits the first value of the source
  34. * Observable, then completes. If called with a `predicate` function, `first`
  35. * emits the first value of the source that matches the specified condition. Emits an error
  36. * notification if `defaultValue` was not provided and a matching element is not found.
  37. *
  38. * ## Examples
  39. *
  40. * Emit only the first click that happens on the DOM
  41. *
  42. * ```ts
  43. * import { fromEvent, first } from 'rxjs';
  44. *
  45. * const clicks = fromEvent(document, 'click');
  46. * const result = clicks.pipe(first());
  47. * result.subscribe(x => console.log(x));
  48. * ```
  49. *
  50. * Emits the first click that happens on a DIV
  51. *
  52. * ```ts
  53. * import { fromEvent, first } from 'rxjs';
  54. *
  55. * const div = document.createElement('div');
  56. * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
  57. * document.body.appendChild(div);
  58. *
  59. * const clicks = fromEvent(document, 'click');
  60. * const result = clicks.pipe(first(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
  61. * result.subscribe(x => console.log(x));
  62. * ```
  63. *
  64. * @see {@link filter}
  65. * @see {@link find}
  66. * @see {@link take}
  67. * @see {@link last}
  68. *
  69. * @throws {EmptyError} Delivers an `EmptyError` to the Observer's `error`
  70. * callback if the Observable completes before any `next` notification was sent.
  71. * This is how `first()` is different from `take(1)` which completes instead.
  72. *
  73. * @param predicate An optional function called with each item to test for condition
  74. * matching.
  75. * @param defaultValue The default value emitted in case no valid value was found on
  76. * the source.
  77. * @return A function that returns an Observable that emits the first item that
  78. * matches the condition.
  79. */
  80. export function first<T, D>(
  81. predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
  82. defaultValue?: D
  83. ): OperatorFunction<T, T | D> {
  84. const hasDefaultValue = arguments.length >= 2;
  85. return (source: Observable<T>) =>
  86. source.pipe(
  87. predicate ? filter((v, i) => predicate(v, i, source)) : identity,
  88. take(1),
  89. hasDefaultValue ? defaultIfEmpty(defaultValue!) : throwIfEmpty(() => new EmptyError())
  90. );
  91. }