a zip code crypto-currency system good for red ONLY

debounce.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { OuterSubscriber } from '../OuterSubscriber';
  2. import { subscribeToResult } from '../util/subscribeToResult';
  3. /**
  4. * Emits a value from the source Observable only after a particular time span
  5. * determined by another Observable has passed without another source emission.
  6. *
  7. * <span class="informal">It's like {@link debounceTime}, but the time span of
  8. * emission silence is determined by a second Observable.</span>
  9. *
  10. * <img src="./img/debounce.png" width="100%">
  11. *
  12. * `debounce` delays values emitted by the source Observable, but drops previous
  13. * pending delayed emissions if a new value arrives on the source Observable.
  14. * This operator keeps track of the most recent value from the source
  15. * Observable, and spawns a duration Observable by calling the
  16. * `durationSelector` function. The value is emitted only when the duration
  17. * Observable emits a value or completes, and if no other value was emitted on
  18. * the source Observable since the duration Observable was spawned. If a new
  19. * value appears before the duration Observable emits, the previous value will
  20. * be dropped and will not be emitted on the output Observable.
  21. *
  22. * Like {@link debounceTime}, this is a rate-limiting operator, and also a
  23. * delay-like operator since output emissions do not necessarily occur at the
  24. * same time as they did on the source Observable.
  25. *
  26. * @example <caption>Emit the most recent click after a burst of clicks</caption>
  27. * var clicks = Rx.Observable.fromEvent(document, 'click');
  28. * var result = clicks.debounce(() => Rx.Observable.interval(1000));
  29. * result.subscribe(x => console.log(x));
  30. *
  31. * @see {@link audit}
  32. * @see {@link debounceTime}
  33. * @see {@link delayWhen}
  34. * @see {@link throttle}
  35. *
  36. * @param {function(value: T): SubscribableOrPromise} durationSelector A function
  37. * that receives a value from the source Observable, for computing the timeout
  38. * duration for each source value, returned as an Observable or a Promise.
  39. * @return {Observable} An Observable that delays the emissions of the source
  40. * Observable by the specified duration Observable returned by
  41. * `durationSelector`, and may drop some values if they occur too frequently.
  42. * @method debounce
  43. * @owner Observable
  44. */
  45. export function debounce(durationSelector) {
  46. return (source) => source.lift(new DebounceOperator(durationSelector));
  47. }
  48. class DebounceOperator {
  49. constructor(durationSelector) {
  50. this.durationSelector = durationSelector;
  51. }
  52. call(subscriber, source) {
  53. return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
  54. }
  55. }
  56. /**
  57. * We need this JSDoc comment for affecting ESDoc.
  58. * @ignore
  59. * @extends {Ignored}
  60. */
  61. class DebounceSubscriber extends OuterSubscriber {
  62. constructor(destination, durationSelector) {
  63. super(destination);
  64. this.durationSelector = durationSelector;
  65. this.hasValue = false;
  66. this.durationSubscription = null;
  67. }
  68. _next(value) {
  69. try {
  70. const result = this.durationSelector.call(this, value);
  71. if (result) {
  72. this._tryNext(value, result);
  73. }
  74. }
  75. catch (err) {
  76. this.destination.error(err);
  77. }
  78. }
  79. _complete() {
  80. this.emitValue();
  81. this.destination.complete();
  82. }
  83. _tryNext(value, duration) {
  84. let subscription = this.durationSubscription;
  85. this.value = value;
  86. this.hasValue = true;
  87. if (subscription) {
  88. subscription.unsubscribe();
  89. this.remove(subscription);
  90. }
  91. subscription = subscribeToResult(this, duration);
  92. if (!subscription.closed) {
  93. this.add(this.durationSubscription = subscription);
  94. }
  95. }
  96. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  97. this.emitValue();
  98. }
  99. notifyComplete() {
  100. this.emitValue();
  101. }
  102. emitValue() {
  103. if (this.hasValue) {
  104. const value = this.value;
  105. const subscription = this.durationSubscription;
  106. if (subscription) {
  107. this.durationSubscription = null;
  108. subscription.unsubscribe();
  109. this.remove(subscription);
  110. }
  111. this.value = null;
  112. this.hasValue = false;
  113. super._next(value);
  114. }
  115. }
  116. }
  117. //# sourceMappingURL=debounce.js.map