takeLast.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /** PURE_IMPORTS_START .._Subscriber,.._util_ArgumentOutOfRangeError,.._observable_EmptyObservable 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. import { EmptyObservable } from '../observable/EmptyObservable';
  12. /**
  13. * Emits only the last `count` values emitted by the source Observable.
  14. *
  15. * <span class="informal">Remembers the latest `count` values, then emits those
  16. * only when the source completes.</span>
  17. *
  18. * <img src="./img/takeLast.png" width="100%">
  19. *
  20. * `takeLast` returns an Observable that emits at most the last `count` values
  21. * emitted by the source Observable. If the source emits fewer than `count`
  22. * values then all of its values are emitted. This operator must wait until the
  23. * `complete` notification emission from the source in order to emit the `next`
  24. * values on the output Observable, because otherwise it is impossible to know
  25. * whether or not more values will be emitted on the source. For this reason,
  26. * all values are emitted synchronously, followed by the complete notification.
  27. *
  28. * @example <caption>Take the last 3 values of an Observable with many values</caption>
  29. * var many = Rx.Observable.range(1, 100);
  30. * var lastThree = many.takeLast(3);
  31. * lastThree.subscribe(x => console.log(x));
  32. *
  33. * @see {@link take}
  34. * @see {@link takeUntil}
  35. * @see {@link takeWhile}
  36. * @see {@link skip}
  37. *
  38. * @throws {ArgumentOutOfRangeError} When using `takeLast(i)`, it delivers an
  39. * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0`.
  40. *
  41. * @param {number} count The maximum number of values to emit from the end of
  42. * the sequence of values emitted by the source Observable.
  43. * @return {Observable<T>} An Observable that emits at most the last count
  44. * values emitted by the source Observable.
  45. * @method takeLast
  46. * @owner Observable
  47. */
  48. export function takeLast(count) {
  49. return function takeLastOperatorFunction(source) {
  50. if (count === 0) {
  51. return new EmptyObservable();
  52. }
  53. else {
  54. return source.lift(new TakeLastOperator(count));
  55. }
  56. };
  57. }
  58. var TakeLastOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  59. function TakeLastOperator(total) {
  60. this.total = total;
  61. if (this.total < 0) {
  62. throw new ArgumentOutOfRangeError;
  63. }
  64. }
  65. TakeLastOperator.prototype.call = function (subscriber, source) {
  66. return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
  67. };
  68. return TakeLastOperator;
  69. }());
  70. /**
  71. * We need this JSDoc comment for affecting ESDoc.
  72. * @ignore
  73. * @extends {Ignored}
  74. */
  75. var TakeLastSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  76. __extends(TakeLastSubscriber, _super);
  77. function TakeLastSubscriber(destination, total) {
  78. _super.call(this, destination);
  79. this.total = total;
  80. this.ring = new Array();
  81. this.count = 0;
  82. }
  83. TakeLastSubscriber.prototype._next = function (value) {
  84. var ring = this.ring;
  85. var total = this.total;
  86. var count = this.count++;
  87. if (ring.length < total) {
  88. ring.push(value);
  89. }
  90. else {
  91. var index = count % total;
  92. ring[index] = value;
  93. }
  94. };
  95. TakeLastSubscriber.prototype._complete = function () {
  96. var destination = this.destination;
  97. var count = this.count;
  98. if (count > 0) {
  99. var total = this.count >= this.total ? this.total : this.count;
  100. var ring = this.ring;
  101. for (var i = 0; i < total; i++) {
  102. var idx = (count++) % total;
  103. destination.next(ring[idx]);
  104. }
  105. }
  106. destination.complete();
  107. };
  108. return TakeLastSubscriber;
  109. }(Subscriber));
  110. //# sourceMappingURL=takeLast.js.map