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

throttleTime.js 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Subscriber_1 = require("../Subscriber");
  17. var async_1 = require("../scheduler/async");
  18. var throttle_1 = require("./throttle");
  19. function throttleTime(duration, scheduler, config) {
  20. if (scheduler === void 0) { scheduler = async_1.async; }
  21. if (config === void 0) { config = throttle_1.defaultThrottleConfig; }
  22. return function (source) { return source.lift(new ThrottleTimeOperator(duration, scheduler, config.leading, config.trailing)); };
  23. }
  24. exports.throttleTime = throttleTime;
  25. var ThrottleTimeOperator = (function () {
  26. function ThrottleTimeOperator(duration, scheduler, leading, trailing) {
  27. this.duration = duration;
  28. this.scheduler = scheduler;
  29. this.leading = leading;
  30. this.trailing = trailing;
  31. }
  32. ThrottleTimeOperator.prototype.call = function (subscriber, source) {
  33. return source.subscribe(new ThrottleTimeSubscriber(subscriber, this.duration, this.scheduler, this.leading, this.trailing));
  34. };
  35. return ThrottleTimeOperator;
  36. }());
  37. var ThrottleTimeSubscriber = (function (_super) {
  38. __extends(ThrottleTimeSubscriber, _super);
  39. function ThrottleTimeSubscriber(destination, duration, scheduler, leading, trailing) {
  40. var _this = _super.call(this, destination) || this;
  41. _this.duration = duration;
  42. _this.scheduler = scheduler;
  43. _this.leading = leading;
  44. _this.trailing = trailing;
  45. _this._hasTrailingValue = false;
  46. _this._trailingValue = null;
  47. return _this;
  48. }
  49. ThrottleTimeSubscriber.prototype._next = function (value) {
  50. if (this.throttled) {
  51. if (this.trailing) {
  52. this._trailingValue = value;
  53. this._hasTrailingValue = true;
  54. }
  55. }
  56. else {
  57. this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.duration, { subscriber: this }));
  58. if (this.leading) {
  59. this.destination.next(value);
  60. }
  61. else if (this.trailing) {
  62. this._trailingValue = value;
  63. this._hasTrailingValue = true;
  64. }
  65. }
  66. };
  67. ThrottleTimeSubscriber.prototype._complete = function () {
  68. if (this._hasTrailingValue) {
  69. this.destination.next(this._trailingValue);
  70. this.destination.complete();
  71. }
  72. else {
  73. this.destination.complete();
  74. }
  75. };
  76. ThrottleTimeSubscriber.prototype.clearThrottle = function () {
  77. var throttled = this.throttled;
  78. if (throttled) {
  79. if (this.trailing && this._hasTrailingValue) {
  80. this.destination.next(this._trailingValue);
  81. this._trailingValue = null;
  82. this._hasTrailingValue = false;
  83. }
  84. throttled.unsubscribe();
  85. this.remove(throttled);
  86. this.throttled = null;
  87. }
  88. };
  89. return ThrottleTimeSubscriber;
  90. }(Subscriber_1.Subscriber));
  91. function dispatchNext(arg) {
  92. var subscriber = arg.subscriber;
  93. subscriber.clearThrottle();
  94. }
  95. //# sourceMappingURL=throttleTime.js.map