audit.js 4.6 KB

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