elementAt.js 3.8 KB

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