throttleTime.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /** PURE_IMPORTS_START tslib,_Subscriber,_scheduler_async,_throttle PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. import { async } from '../scheduler/async';
  5. import { defaultThrottleConfig } from './throttle';
  6. export function throttleTime(duration, scheduler, config) {
  7. if (scheduler === void 0) {
  8. scheduler = async;
  9. }
  10. if (config === void 0) {
  11. config = defaultThrottleConfig;
  12. }
  13. return function (source) { return source.lift(new ThrottleTimeOperator(duration, scheduler, config.leading, config.trailing)); };
  14. }
  15. var ThrottleTimeOperator = /*@__PURE__*/ (function () {
  16. function ThrottleTimeOperator(duration, scheduler, leading, trailing) {
  17. this.duration = duration;
  18. this.scheduler = scheduler;
  19. this.leading = leading;
  20. this.trailing = trailing;
  21. }
  22. ThrottleTimeOperator.prototype.call = function (subscriber, source) {
  23. return source.subscribe(new ThrottleTimeSubscriber(subscriber, this.duration, this.scheduler, this.leading, this.trailing));
  24. };
  25. return ThrottleTimeOperator;
  26. }());
  27. var ThrottleTimeSubscriber = /*@__PURE__*/ (function (_super) {
  28. tslib_1.__extends(ThrottleTimeSubscriber, _super);
  29. function ThrottleTimeSubscriber(destination, duration, scheduler, leading, trailing) {
  30. var _this = _super.call(this, destination) || this;
  31. _this.duration = duration;
  32. _this.scheduler = scheduler;
  33. _this.leading = leading;
  34. _this.trailing = trailing;
  35. _this._hasTrailingValue = false;
  36. _this._trailingValue = null;
  37. return _this;
  38. }
  39. ThrottleTimeSubscriber.prototype._next = function (value) {
  40. if (this.throttled) {
  41. if (this.trailing) {
  42. this._trailingValue = value;
  43. this._hasTrailingValue = true;
  44. }
  45. }
  46. else {
  47. this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.duration, { subscriber: this }));
  48. if (this.leading) {
  49. this.destination.next(value);
  50. }
  51. else if (this.trailing) {
  52. this._trailingValue = value;
  53. this._hasTrailingValue = true;
  54. }
  55. }
  56. };
  57. ThrottleTimeSubscriber.prototype._complete = function () {
  58. if (this._hasTrailingValue) {
  59. this.destination.next(this._trailingValue);
  60. this.destination.complete();
  61. }
  62. else {
  63. this.destination.complete();
  64. }
  65. };
  66. ThrottleTimeSubscriber.prototype.clearThrottle = function () {
  67. var throttled = this.throttled;
  68. if (throttled) {
  69. if (this.trailing && this._hasTrailingValue) {
  70. this.destination.next(this._trailingValue);
  71. this._trailingValue = null;
  72. this._hasTrailingValue = false;
  73. }
  74. throttled.unsubscribe();
  75. this.remove(throttled);
  76. this.throttled = null;
  77. }
  78. };
  79. return ThrottleTimeSubscriber;
  80. }(Subscriber));
  81. function dispatchNext(arg) {
  82. var subscriber = arg.subscriber;
  83. subscriber.clearThrottle();
  84. }
  85. //# sourceMappingURL=throttleTime.js.map