skipLast.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * Skip the last `count` values emitted by the source Observable.
  13. *
  14. * <img src="./img/skipLast.png" width="100%">
  15. *
  16. * `skipLast` returns an Observable that accumulates a queue with a length
  17. * enough to store the first `count` values. As more values are received,
  18. * values are taken from the front of the queue and produced on the result
  19. * sequence. This causes values to be delayed.
  20. *
  21. * @example <caption>Skip the last 2 values of an Observable with many values</caption>
  22. * var many = Rx.Observable.range(1, 5);
  23. * var skipLastTwo = many.skipLast(2);
  24. * skipLastTwo.subscribe(x => console.log(x));
  25. *
  26. * // Results in:
  27. * // 1 2 3
  28. *
  29. * @see {@link skip}
  30. * @see {@link skipUntil}
  31. * @see {@link skipWhile}
  32. * @see {@link take}
  33. *
  34. * @throws {ArgumentOutOfRangeError} When using `skipLast(i)`, it throws
  35. * ArgumentOutOrRangeError if `i < 0`.
  36. *
  37. * @param {number} count Number of elements to skip from the end of the source Observable.
  38. * @returns {Observable<T>} An Observable that skips the last count values
  39. * emitted by the source Observable.
  40. * @method skipLast
  41. * @owner Observable
  42. */
  43. export function skipLast(count) {
  44. return function (source) { return source.lift(new SkipLastOperator(count)); };
  45. }
  46. var SkipLastOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  47. function SkipLastOperator(_skipCount) {
  48. this._skipCount = _skipCount;
  49. if (this._skipCount < 0) {
  50. throw new ArgumentOutOfRangeError;
  51. }
  52. }
  53. SkipLastOperator.prototype.call = function (subscriber, source) {
  54. if (this._skipCount === 0) {
  55. // If we don't want to skip any values then just subscribe
  56. // to Subscriber without any further logic.
  57. return source.subscribe(new Subscriber(subscriber));
  58. }
  59. else {
  60. return source.subscribe(new SkipLastSubscriber(subscriber, this._skipCount));
  61. }
  62. };
  63. return SkipLastOperator;
  64. }());
  65. /**
  66. * We need this JSDoc comment for affecting ESDoc.
  67. * @ignore
  68. * @extends {Ignored}
  69. */
  70. var SkipLastSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  71. __extends(SkipLastSubscriber, _super);
  72. function SkipLastSubscriber(destination, _skipCount) {
  73. _super.call(this, destination);
  74. this._skipCount = _skipCount;
  75. this._count = 0;
  76. this._ring = new Array(_skipCount);
  77. }
  78. SkipLastSubscriber.prototype._next = function (value) {
  79. var skipCount = this._skipCount;
  80. var count = this._count++;
  81. if (count < skipCount) {
  82. this._ring[count] = value;
  83. }
  84. else {
  85. var currentIndex = count % skipCount;
  86. var ring = this._ring;
  87. var oldValue = ring[currentIndex];
  88. ring[currentIndex] = value;
  89. this.destination.next(oldValue);
  90. }
  91. };
  92. return SkipLastSubscriber;
  93. }(Subscriber));
  94. //# sourceMappingURL=skipLast.js.map