Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

throttle.js 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /** PURE_IMPORTS_START tslib,_OuterSubscriber,_util_subscribeToResult PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { OuterSubscriber } from '../OuterSubscriber';
  4. import { subscribeToResult } from '../util/subscribeToResult';
  5. export var defaultThrottleConfig = {
  6. leading: true,
  7. trailing: false
  8. };
  9. export function throttle(durationSelector, config) {
  10. if (config === void 0) {
  11. config = defaultThrottleConfig;
  12. }
  13. return function (source) { return source.lift(new ThrottleOperator(durationSelector, config.leading, config.trailing)); };
  14. }
  15. var ThrottleOperator = /*@__PURE__*/ (function () {
  16. function ThrottleOperator(durationSelector, leading, trailing) {
  17. this.durationSelector = durationSelector;
  18. this.leading = leading;
  19. this.trailing = trailing;
  20. }
  21. ThrottleOperator.prototype.call = function (subscriber, source) {
  22. return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
  23. };
  24. return ThrottleOperator;
  25. }());
  26. var ThrottleSubscriber = /*@__PURE__*/ (function (_super) {
  27. tslib_1.__extends(ThrottleSubscriber, _super);
  28. function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) {
  29. var _this = _super.call(this, destination) || this;
  30. _this.destination = destination;
  31. _this.durationSelector = durationSelector;
  32. _this._leading = _leading;
  33. _this._trailing = _trailing;
  34. _this._hasValue = false;
  35. return _this;
  36. }
  37. ThrottleSubscriber.prototype._next = function (value) {
  38. this._hasValue = true;
  39. this._sendValue = value;
  40. if (!this._throttled) {
  41. if (this._leading) {
  42. this.send();
  43. }
  44. else {
  45. this.throttle(value);
  46. }
  47. }
  48. };
  49. ThrottleSubscriber.prototype.send = function () {
  50. var _a = this, _hasValue = _a._hasValue, _sendValue = _a._sendValue;
  51. if (_hasValue) {
  52. this.destination.next(_sendValue);
  53. this.throttle(_sendValue);
  54. }
  55. this._hasValue = false;
  56. this._sendValue = null;
  57. };
  58. ThrottleSubscriber.prototype.throttle = function (value) {
  59. var duration = this.tryDurationSelector(value);
  60. if (!!duration) {
  61. this.add(this._throttled = subscribeToResult(this, duration));
  62. }
  63. };
  64. ThrottleSubscriber.prototype.tryDurationSelector = function (value) {
  65. try {
  66. return this.durationSelector(value);
  67. }
  68. catch (err) {
  69. this.destination.error(err);
  70. return null;
  71. }
  72. };
  73. ThrottleSubscriber.prototype.throttlingDone = function () {
  74. var _a = this, _throttled = _a._throttled, _trailing = _a._trailing;
  75. if (_throttled) {
  76. _throttled.unsubscribe();
  77. }
  78. this._throttled = null;
  79. if (_trailing) {
  80. this.send();
  81. }
  82. };
  83. ThrottleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  84. this.throttlingDone();
  85. };
  86. ThrottleSubscriber.prototype.notifyComplete = function () {
  87. this.throttlingDone();
  88. };
  89. return ThrottleSubscriber;
  90. }(OuterSubscriber));
  91. //# sourceMappingURL=throttle.js.map