audit.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /** PURE_IMPORTS_START tslib,_innerSubscribe PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  4. export function audit(durationSelector) {
  5. return function auditOperatorFunction(source) {
  6. return source.lift(new AuditOperator(durationSelector));
  7. };
  8. }
  9. var AuditOperator = /*@__PURE__*/ (function () {
  10. function AuditOperator(durationSelector) {
  11. this.durationSelector = durationSelector;
  12. }
  13. AuditOperator.prototype.call = function (subscriber, source) {
  14. return source.subscribe(new AuditSubscriber(subscriber, this.durationSelector));
  15. };
  16. return AuditOperator;
  17. }());
  18. var AuditSubscriber = /*@__PURE__*/ (function (_super) {
  19. tslib_1.__extends(AuditSubscriber, _super);
  20. function AuditSubscriber(destination, durationSelector) {
  21. var _this = _super.call(this, destination) || this;
  22. _this.durationSelector = durationSelector;
  23. _this.hasValue = false;
  24. return _this;
  25. }
  26. AuditSubscriber.prototype._next = function (value) {
  27. this.value = value;
  28. this.hasValue = true;
  29. if (!this.throttled) {
  30. var duration = void 0;
  31. try {
  32. var durationSelector = this.durationSelector;
  33. duration = durationSelector(value);
  34. }
  35. catch (err) {
  36. return this.destination.error(err);
  37. }
  38. var innerSubscription = innerSubscribe(duration, new SimpleInnerSubscriber(this));
  39. if (!innerSubscription || innerSubscription.closed) {
  40. this.clearThrottle();
  41. }
  42. else {
  43. this.add(this.throttled = innerSubscription);
  44. }
  45. }
  46. };
  47. AuditSubscriber.prototype.clearThrottle = function () {
  48. var _a = this, value = _a.value, hasValue = _a.hasValue, throttled = _a.throttled;
  49. if (throttled) {
  50. this.remove(throttled);
  51. this.throttled = undefined;
  52. throttled.unsubscribe();
  53. }
  54. if (hasValue) {
  55. this.value = undefined;
  56. this.hasValue = false;
  57. this.destination.next(value);
  58. }
  59. };
  60. AuditSubscriber.prototype.notifyNext = function () {
  61. this.clearThrottle();
  62. };
  63. AuditSubscriber.prototype.notifyComplete = function () {
  64. this.clearThrottle();
  65. };
  66. return AuditSubscriber;
  67. }(SimpleOuterSubscriber));
  68. //# sourceMappingURL=audit.js.map