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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var OuterSubscriber_1 = require("../OuterSubscriber");
  17. var subscribeToResult_1 = require("../util/subscribeToResult");
  18. exports.defaultThrottleConfig = {
  19. leading: true,
  20. trailing: false
  21. };
  22. function throttle(durationSelector, config) {
  23. if (config === void 0) { config = exports.defaultThrottleConfig; }
  24. return function (source) { return source.lift(new ThrottleOperator(durationSelector, config.leading, config.trailing)); };
  25. }
  26. exports.throttle = throttle;
  27. var ThrottleOperator = (function () {
  28. function ThrottleOperator(durationSelector, leading, trailing) {
  29. this.durationSelector = durationSelector;
  30. this.leading = leading;
  31. this.trailing = trailing;
  32. }
  33. ThrottleOperator.prototype.call = function (subscriber, source) {
  34. return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
  35. };
  36. return ThrottleOperator;
  37. }());
  38. var ThrottleSubscriber = (function (_super) {
  39. __extends(ThrottleSubscriber, _super);
  40. function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) {
  41. var _this = _super.call(this, destination) || this;
  42. _this.destination = destination;
  43. _this.durationSelector = durationSelector;
  44. _this._leading = _leading;
  45. _this._trailing = _trailing;
  46. _this._hasValue = false;
  47. return _this;
  48. }
  49. ThrottleSubscriber.prototype._next = function (value) {
  50. this._hasValue = true;
  51. this._sendValue = value;
  52. if (!this._throttled) {
  53. if (this._leading) {
  54. this.send();
  55. }
  56. else {
  57. this.throttle(value);
  58. }
  59. }
  60. };
  61. ThrottleSubscriber.prototype.send = function () {
  62. var _a = this, _hasValue = _a._hasValue, _sendValue = _a._sendValue;
  63. if (_hasValue) {
  64. this.destination.next(_sendValue);
  65. this.throttle(_sendValue);
  66. }
  67. this._hasValue = false;
  68. this._sendValue = null;
  69. };
  70. ThrottleSubscriber.prototype.throttle = function (value) {
  71. var duration = this.tryDurationSelector(value);
  72. if (!!duration) {
  73. this.add(this._throttled = subscribeToResult_1.subscribeToResult(this, duration));
  74. }
  75. };
  76. ThrottleSubscriber.prototype.tryDurationSelector = function (value) {
  77. try {
  78. return this.durationSelector(value);
  79. }
  80. catch (err) {
  81. this.destination.error(err);
  82. return null;
  83. }
  84. };
  85. ThrottleSubscriber.prototype.throttlingDone = function () {
  86. var _a = this, _throttled = _a._throttled, _trailing = _a._trailing;
  87. if (_throttled) {
  88. _throttled.unsubscribe();
  89. }
  90. this._throttled = null;
  91. if (_trailing) {
  92. this.send();
  93. }
  94. };
  95. ThrottleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  96. this.throttlingDone();
  97. };
  98. ThrottleSubscriber.prototype.notifyComplete = function () {
  99. this.throttlingDone();
  100. };
  101. return ThrottleSubscriber;
  102. }(OuterSubscriber_1.OuterSubscriber));
  103. //# sourceMappingURL=throttle.js.map