Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

elementAt.ts 2.6KB

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