c7d91f812a1404499ee32805c4d5f371e20e663dfcff2497bdbfa89b28e2ae5e5f153cdaab395d25f9eb26739ec1d0d88aaef46b66bc2e81ff3acf596a55b9 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  2. import { Observable } from '../Observable';
  3. import { OperatorFunction } from '../types';
  4. import { filter } from './filter';
  5. import { throwIfEmpty } from './throwIfEmpty';
  6. import { defaultIfEmpty } from './defaultIfEmpty';
  7. import { take } from './take';
  8. /**
  9. * Emits the single value at the specified `index` in a sequence of emissions
  10. * from the source Observable.
  11. *
  12. * <span class="informal">Emits only the i-th value, then completes.</span>
  13. *
  14. * ![](elementAt.png)
  15. *
  16. * `elementAt` returns an Observable that emits the item at the specified
  17. * `index` in the source Observable, or a default value if that `index` is out
  18. * of range and the `default` argument is provided. If the `default` argument is
  19. * not given and the `index` is out of range, the output Observable will emit an
  20. * `ArgumentOutOfRangeError` error.
  21. *
  22. * ## Example
  23. *
  24. * Emit only the third click event
  25. *
  26. * ```ts
  27. * import { fromEvent, elementAt } from 'rxjs';
  28. *
  29. * const clicks = fromEvent(document, 'click');
  30. * const result = clicks.pipe(elementAt(2));
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * // Results in:
  34. * // click 1 = nothing
  35. * // click 2 = nothing
  36. * // click 3 = MouseEvent object logged to console
  37. * ```
  38. *
  39. * @see {@link first}
  40. * @see {@link last}
  41. * @see {@link skip}
  42. * @see {@link single}
  43. * @see {@link take}
  44. *
  45. * @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an
  46. * `ArgumentOutOfRangeError` to the Observer's `error` callback if `i < 0` or the
  47. * Observable has completed before emitting the i-th `next` notification.
  48. *
  49. * @param index Is the number `i` for the i-th source emission that has happened
  50. * since the subscription, starting from the number `0`.
  51. * @param defaultValue The default value returned for missing indices.
  52. * @return A function that returns an Observable that emits a single item, if
  53. * it is found. Otherwise, it will emit the default value if given. If not, it
  54. * emits an error.
  55. */
  56. export function elementAt<T, D = T>(index: number, defaultValue?: D): OperatorFunction<T, T | D> {
  57. if (index < 0) {
  58. throw new ArgumentOutOfRangeError();
  59. }
  60. const hasDefaultValue = arguments.length >= 2;
  61. return (source: Observable<T>) =>
  62. source.pipe(
  63. filter((v, i) => i === index),
  64. take(1),
  65. hasDefaultValue ? defaultIfEmpty(defaultValue!) : throwIfEmpty(() => new ArgumentOutOfRangeError())
  66. );
  67. }