audit.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /** PURE_IMPORTS_START .._util_tryCatch,.._util_errorObject,.._OuterSubscriber,.._util_subscribeToResult 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 { tryCatch } from '../util/tryCatch';
  10. import { errorObject } from '../util/errorObject';
  11. import { OuterSubscriber } from '../OuterSubscriber';
  12. import { subscribeToResult } from '../util/subscribeToResult';
  13. /**
  14. * Ignores source values for a duration determined by another Observable, then
  15. * emits the most recent value from the source Observable, then repeats this
  16. * process.
  17. *
  18. * <span class="informal">It's like {@link auditTime}, but the silencing
  19. * duration is determined by a second Observable.</span>
  20. *
  21. * <img src="./img/audit.png" width="100%">
  22. *
  23. * `audit` is similar to `throttle`, but emits the last value from the silenced
  24. * time window, instead of the first value. `audit` emits the most recent value
  25. * from the source Observable on the output Observable as soon as its internal
  26. * timer becomes disabled, and ignores source values while the timer is enabled.
  27. * Initially, the timer is disabled. As soon as the first source value arrives,
  28. * the timer is enabled by calling the `durationSelector` function with the
  29. * source value, which returns the "duration" Observable. When the duration
  30. * Observable emits a value or completes, the timer is disabled, then the most
  31. * recent source value is emitted on the output Observable, and this process
  32. * repeats for the next source value.
  33. *
  34. * @example <caption>Emit clicks at a rate of at most one click per second</caption>
  35. * var clicks = Rx.Observable.fromEvent(document, 'click');
  36. * var result = clicks.audit(ev => Rx.Observable.interval(1000));
  37. * result.subscribe(x => console.log(x));
  38. *
  39. * @see {@link auditTime}
  40. * @see {@link debounce}
  41. * @see {@link delayWhen}
  42. * @see {@link sample}
  43. * @see {@link throttle}
  44. *
  45. * @param {function(value: T): SubscribableOrPromise} durationSelector A function
  46. * that receives a value from the source Observable, for computing the silencing
  47. * duration, returned as an Observable or a Promise.
  48. * @return {Observable<T>} An Observable that performs rate-limiting of
  49. * emissions from the source Observable.
  50. * @method audit
  51. * @owner Observable
  52. */
  53. export function audit(durationSelector) {
  54. return function auditOperatorFunction(source) {
  55. return source.lift(new AuditOperator(durationSelector));
  56. };
  57. }
  58. var AuditOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  59. function AuditOperator(durationSelector) {
  60. this.durationSelector = durationSelector;
  61. }
  62. AuditOperator.prototype.call = function (subscriber, source) {
  63. return source.subscribe(new AuditSubscriber(subscriber, this.durationSelector));
  64. };
  65. return AuditOperator;
  66. }());
  67. /**
  68. * We need this JSDoc comment for affecting ESDoc.
  69. * @ignore
  70. * @extends {Ignored}
  71. */
  72. var AuditSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  73. __extends(AuditSubscriber, _super);
  74. function AuditSubscriber(destination, durationSelector) {
  75. _super.call(this, destination);
  76. this.durationSelector = durationSelector;
  77. this.hasValue = false;
  78. }
  79. AuditSubscriber.prototype._next = function (value) {
  80. this.value = value;
  81. this.hasValue = true;
  82. if (!this.throttled) {
  83. var duration = tryCatch(this.durationSelector)(value);
  84. if (duration === errorObject) {
  85. this.destination.error(errorObject.e);
  86. }
  87. else {
  88. var innerSubscription = subscribeToResult(this, duration);
  89. if (innerSubscription.closed) {
  90. this.clearThrottle();
  91. }
  92. else {
  93. this.add(this.throttled = innerSubscription);
  94. }
  95. }
  96. }
  97. };
  98. AuditSubscriber.prototype.clearThrottle = function () {
  99. var _a = this, value = _a.value, hasValue = _a.hasValue, throttled = _a.throttled;
  100. if (throttled) {
  101. this.remove(throttled);
  102. this.throttled = null;
  103. throttled.unsubscribe();
  104. }
  105. if (hasValue) {
  106. this.value = null;
  107. this.hasValue = false;
  108. this.destination.next(value);
  109. }
  110. };
  111. AuditSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex) {
  112. this.clearThrottle();
  113. };
  114. AuditSubscriber.prototype.notifyComplete = function () {
  115. this.clearThrottle();
  116. };
  117. return AuditSubscriber;
  118. }(OuterSubscriber));
  119. //# sourceMappingURL=audit.js.map