a zip code crypto-currency system good for red ONLY

throttle.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 OuterSubscriber_1 = require('../OuterSubscriber');
  8. var subscribeToResult_1 = require('../util/subscribeToResult');
  9. exports.defaultThrottleConfig = {
  10. leading: true,
  11. trailing: false
  12. };
  13. /**
  14. * Emits a value from the source Observable, then ignores subsequent source
  15. * values for a duration determined by another Observable, then repeats this
  16. * process.
  17. *
  18. * <span class="informal">It's like {@link throttleTime}, but the silencing
  19. * duration is determined by a second Observable.</span>
  20. *
  21. * <img src="./img/throttle.png" width="100%">
  22. *
  23. * `throttle` emits the source Observable values on the output Observable
  24. * when its internal timer is disabled, and ignores source values when the timer
  25. * is enabled. Initially, the timer is disabled. As soon as the first source
  26. * value arrives, it is forwarded to the output Observable, and then the timer
  27. * is enabled by calling the `durationSelector` function with the source value,
  28. * which returns the "duration" Observable. When the duration Observable emits a
  29. * value or completes, the timer is disabled, and this process repeats for the
  30. * next source value.
  31. *
  32. * @example <caption>Emit clicks at a rate of at most one click per second</caption>
  33. * var clicks = Rx.Observable.fromEvent(document, 'click');
  34. * var result = clicks.throttle(ev => Rx.Observable.interval(1000));
  35. * result.subscribe(x => console.log(x));
  36. *
  37. * @see {@link audit}
  38. * @see {@link debounce}
  39. * @see {@link delayWhen}
  40. * @see {@link sample}
  41. * @see {@link throttleTime}
  42. *
  43. * @param {function(value: T): SubscribableOrPromise} durationSelector A function
  44. * that receives a value from the source Observable, for computing the silencing
  45. * duration for each source value, returned as an Observable or a Promise.
  46. * @param {Object} config a configuration object to define `leading` and `trailing` behavior. Defaults
  47. * to `{ leading: true, trailing: false }`.
  48. * @return {Observable<T>} An Observable that performs the throttle operation to
  49. * limit the rate of emissions from the source.
  50. * @method throttle
  51. * @owner Observable
  52. */
  53. function throttle(durationSelector, config) {
  54. if (config === void 0) { config = exports.defaultThrottleConfig; }
  55. return function (source) { return source.lift(new ThrottleOperator(durationSelector, config.leading, config.trailing)); };
  56. }
  57. exports.throttle = throttle;
  58. var ThrottleOperator = (function () {
  59. function ThrottleOperator(durationSelector, leading, trailing) {
  60. this.durationSelector = durationSelector;
  61. this.leading = leading;
  62. this.trailing = trailing;
  63. }
  64. ThrottleOperator.prototype.call = function (subscriber, source) {
  65. return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
  66. };
  67. return ThrottleOperator;
  68. }());
  69. /**
  70. * We need this JSDoc comment for affecting ESDoc
  71. * @ignore
  72. * @extends {Ignored}
  73. */
  74. var ThrottleSubscriber = (function (_super) {
  75. __extends(ThrottleSubscriber, _super);
  76. function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) {
  77. _super.call(this, destination);
  78. this.destination = destination;
  79. this.durationSelector = durationSelector;
  80. this._leading = _leading;
  81. this._trailing = _trailing;
  82. this._hasTrailingValue = false;
  83. }
  84. ThrottleSubscriber.prototype._next = function (value) {
  85. if (this.throttled) {
  86. if (this._trailing) {
  87. this._hasTrailingValue = true;
  88. this._trailingValue = value;
  89. }
  90. }
  91. else {
  92. var duration = this.tryDurationSelector(value);
  93. if (duration) {
  94. this.add(this.throttled = subscribeToResult_1.subscribeToResult(this, duration));
  95. }
  96. if (this._leading) {
  97. this.destination.next(value);
  98. if (this._trailing) {
  99. this._hasTrailingValue = true;
  100. this._trailingValue = value;
  101. }
  102. }
  103. }
  104. };
  105. ThrottleSubscriber.prototype.tryDurationSelector = function (value) {
  106. try {
  107. return this.durationSelector(value);
  108. }
  109. catch (err) {
  110. this.destination.error(err);
  111. return null;
  112. }
  113. };
  114. /** @deprecated internal use only */ ThrottleSubscriber.prototype._unsubscribe = function () {
  115. var _a = this, throttled = _a.throttled, _trailingValue = _a._trailingValue, _hasTrailingValue = _a._hasTrailingValue, _trailing = _a._trailing;
  116. this._trailingValue = null;
  117. this._hasTrailingValue = false;
  118. if (throttled) {
  119. this.remove(throttled);
  120. this.throttled = null;
  121. throttled.unsubscribe();
  122. }
  123. };
  124. ThrottleSubscriber.prototype._sendTrailing = function () {
  125. var _a = this, destination = _a.destination, throttled = _a.throttled, _trailing = _a._trailing, _trailingValue = _a._trailingValue, _hasTrailingValue = _a._hasTrailingValue;
  126. if (throttled && _trailing && _hasTrailingValue) {
  127. destination.next(_trailingValue);
  128. this._trailingValue = null;
  129. this._hasTrailingValue = false;
  130. }
  131. };
  132. ThrottleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  133. this._sendTrailing();
  134. this._unsubscribe();
  135. };
  136. ThrottleSubscriber.prototype.notifyComplete = function () {
  137. this._sendTrailing();
  138. this._unsubscribe();
  139. };
  140. return ThrottleSubscriber;
  141. }(OuterSubscriber_1.OuterSubscriber));
  142. //# sourceMappingURL=throttle.js.map