a zip code crypto-currency system good for red ONLY

takeLast.js 3.9KB

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