6afd221de751aae6cc586dac476dd136836c3fbea4b529a5c5b26138b274350a5924de66aa236be4a3dbaab748ca1e3e258b714a2578e85f5bded3cbf93320 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Observable } from '../Observable';
  2. import { Operator } from '../Operator';
  3. import { Subscriber } from '../Subscriber';
  4. import { EmptyError } from '../util/EmptyError';
  5. import { OperatorFunction } from '../../internal/types';
  6. import { filter } from './filter';
  7. import { take } from './take';
  8. import { defaultIfEmpty } from './defaultIfEmpty';
  9. import { throwIfEmpty } from './throwIfEmpty';
  10. import { identity } from '../util/identity';
  11. /* tslint:disable:max-line-length */
  12. export function first<T, D = T>(
  13. predicate?: null,
  14. defaultValue?: D
  15. ): OperatorFunction<T, T | D>;
  16. export function first<T, S extends T>(
  17. predicate: (value: T, index: number, source: Observable<T>) => value is S,
  18. defaultValue?: S
  19. ): OperatorFunction<T, S>;
  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. /* tslint:enable:max-line-length */
  25. /**
  26. * Emits only the first value (or the first value that meets some condition)
  27. * emitted by the source Observable.
  28. *
  29. * <span class="informal">Emits only the first value. Or emits only the first
  30. * value that passes some test.</span>
  31. *
  32. * ![](first.png)
  33. *
  34. * If called with no arguments, `first` emits the first value of the source
  35. * Observable, then completes. If called with a `predicate` function, `first`
  36. * emits the first value of the source that matches the specified condition. It
  37. * may also take a deprecated `resultSelector` function to produce the output
  38. * value from the input value, and a `defaultValue` to emit in case the source
  39. * completes before it is able to emit a valid value. Throws an error if
  40. * `defaultValue` was not provided and a matching element is not found.
  41. *
  42. * ## Examples
  43. * Emit only the first click that happens on the DOM
  44. * ```ts
  45. * import { fromEvent } from 'rxjs';
  46. * import { first } from 'rxjs/operators';
  47. *
  48. * const clicks = fromEvent(document, 'click');
  49. * const result = clicks.pipe(first());
  50. * result.subscribe(x => console.log(x));
  51. * ```
  52. *
  53. * Emits the first click that happens on a DIV
  54. * ```ts
  55. * import { fromEvent } from 'rxjs';
  56. * import { first } from 'rxjs/operators';
  57. *
  58. * const clicks = fromEvent(document, 'click');
  59. * const result = clicks.pipe(first(ev => ev.target.tagName === 'DIV'));
  60. * result.subscribe(x => console.log(x));
  61. * ```
  62. *
  63. * @see {@link filter}
  64. * @see {@link find}
  65. * @see {@link take}
  66. *
  67. * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
  68. * callback if the Observable completes before any `next` notification was sent.
  69. *
  70. * @param {function(value: T, index: number, source: Observable<T>): boolean} [predicate]
  71. * An optional function called with each item to test for condition matching.
  72. * @param {R} [defaultValue] The default value emitted in case no valid value
  73. * was found on the source.
  74. * @return {Observable<T|R>} An Observable of the first item that matches the
  75. * condition.
  76. * @method first
  77. * @owner Observable
  78. */
  79. export function first<T, D>(
  80. predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
  81. defaultValue?: D
  82. ): OperatorFunction<T, T | D> {
  83. const hasDefaultValue = arguments.length >= 2;
  84. return (source: Observable<T>) => source.pipe(
  85. predicate ? filter((v, i) => predicate(v, i, source)) : identity,
  86. take(1),
  87. hasDefaultValue ? defaultIfEmpty<T | D>(defaultValue) : throwIfEmpty(() => new EmptyError()),
  88. );
  89. }