elementAt.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /** PURE_IMPORTS_START .._operators_elementAt PURE_IMPORTS_END */
  2. import { elementAt as higherOrder } from '../operators/elementAt';
  3. /**
  4. * Emits the single value at the specified `index` in a sequence of emissions
  5. * from the source Observable.
  6. *
  7. * <span class="informal">Emits only the i-th value, then completes.</span>
  8. *
  9. * <img src="./img/elementAt.png" width="100%">
  10. *
  11. * `elementAt` returns an Observable that emits the item at the specified
  12. * `index` in the source Observable, or a default value if that `index` is out
  13. * of range and the `default` argument is provided. If the `default` argument is
  14. * not given and the `index` is out of range, the output Observable will emit an
  15. * `ArgumentOutOfRangeError` error.
  16. *
  17. * @example <caption>Emit only the third click event</caption>
  18. * var clicks = Rx.Observable.fromEvent(document, 'click');
  19. * var result = clicks.elementAt(2);
  20. * result.subscribe(x => console.log(x));
  21. *
  22. * // Results in:
  23. * // click 1 = nothing
  24. * // click 2 = nothing
  25. * // click 3 = MouseEvent object logged to console
  26. *
  27. * @see {@link first}
  28. * @see {@link last}
  29. * @see {@link skip}
  30. * @see {@link single}
  31. * @see {@link take}
  32. *
  33. * @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an
  34. * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0` or the
  35. * Observable has completed before emitting the i-th `next` notification.
  36. *
  37. * @param {number} index Is the number `i` for the i-th source emission that has
  38. * happened since the subscription, starting from the number `0`.
  39. * @param {T} [defaultValue] The default value returned for missing indices.
  40. * @return {Observable} An Observable that emits a single item, if it is found.
  41. * Otherwise, will emit the default value if given. If not, then emits an error.
  42. * @method elementAt
  43. * @owner Observable
  44. */
  45. export function elementAt(index, defaultValue) {
  46. return higherOrder(index, defaultValue)(this);
  47. }
  48. //# sourceMappingURL=elementAt.js.map