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

throttle.js 3.7KB

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