Front end of the Slack clone application.

elementAt.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /** PURE_IMPORTS_START .._Subscriber,.._util_ArgumentOutOfRangeError PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subscriber } from '../Subscriber';
  10. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  11. /**
  12. * Emits the single value at the specified `index` in a sequence of emissions
  13. * from the source Observable.
  14. *
  15. * <span class="informal">Emits only the i-th value, then completes.</span>
  16. *
  17. * <img src="./img/elementAt.png" width="100%">
  18. *
  19. * `elementAt` returns an Observable that emits the item at the specified
  20. * `index` in the source Observable, or a default value if that `index` is out
  21. * of range and the `default` argument is provided. If the `default` argument is
  22. * not given and the `index` is out of range, the output Observable will emit an
  23. * `ArgumentOutOfRangeError` error.
  24. *
  25. * @example <caption>Emit only the third click event</caption>
  26. * var clicks = Rx.Observable.fromEvent(document, 'click');
  27. * var result = clicks.elementAt(2);
  28. * result.subscribe(x => console.log(x));
  29. *
  30. * // Results in:
  31. * // click 1 = nothing
  32. * // click 2 = nothing
  33. * // click 3 = MouseEvent object logged to console
  34. *
  35. * @see {@link first}
  36. * @see {@link last}
  37. * @see {@link skip}
  38. * @see {@link single}
  39. * @see {@link take}
  40. *
  41. * @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an
  42. * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0` or the
  43. * Observable has completed before emitting the i-th `next` notification.
  44. *
  45. * @param {number} index Is the number `i` for the i-th source emission that has
  46. * happened since the subscription, starting from the number `0`.
  47. * @param {T} [defaultValue] The default value returned for missing indices.
  48. * @return {Observable} An Observable that emits a single item, if it is found.
  49. * Otherwise, will emit the default value if given. If not, then emits an error.
  50. * @method elementAt
  51. * @owner Observable
  52. */
  53. export function elementAt(index, defaultValue) {
  54. return function (source) { return source.lift(new ElementAtOperator(index, defaultValue)); };
  55. }
  56. var ElementAtOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  57. function ElementAtOperator(index, defaultValue) {
  58. this.index = index;
  59. this.defaultValue = defaultValue;
  60. if (index < 0) {
  61. throw new ArgumentOutOfRangeError;
  62. }
  63. }
  64. ElementAtOperator.prototype.call = function (subscriber, source) {
  65. return source.subscribe(new ElementAtSubscriber(subscriber, this.index, this.defaultValue));
  66. };
  67. return ElementAtOperator;
  68. }());
  69. /**
  70. * We need this JSDoc comment for affecting ESDoc.
  71. * @ignore
  72. * @extends {Ignored}
  73. */
  74. var ElementAtSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  75. __extends(ElementAtSubscriber, _super);
  76. function ElementAtSubscriber(destination, index, defaultValue) {
  77. _super.call(this, destination);
  78. this.index = index;
  79. this.defaultValue = defaultValue;
  80. }
  81. ElementAtSubscriber.prototype._next = function (x) {
  82. if (this.index-- === 0) {
  83. this.destination.next(x);
  84. this.destination.complete();
  85. }
  86. };
  87. ElementAtSubscriber.prototype._complete = function () {
  88. var destination = this.destination;
  89. if (this.index >= 0) {
  90. if (typeof this.defaultValue !== 'undefined') {
  91. destination.next(this.defaultValue);
  92. }
  93. else {
  94. destination.error(new ArgumentOutOfRangeError);
  95. }
  96. }
  97. destination.complete();
  98. };
  99. return ElementAtSubscriber;
  100. }(Subscriber));
  101. //# sourceMappingURL=elementAt.js.map