skipLast.js 3.2 KB

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