Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

throttle.js 3.2KB

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