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.

throttleTime.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  62. };
  63. ThrottleTimeSubscriber.prototype._complete = function () {
  64. if (this._hasTrailingValue) {
  65. this.destination.next(this._trailingValue);
  66. this.destination.complete();
  67. }
  68. else {
  69. this.destination.complete();
  70. }
  71. };
  72. ThrottleTimeSubscriber.prototype.clearThrottle = function () {
  73. var throttled = this.throttled;
  74. if (throttled) {
  75. if (this.trailing && this._hasTrailingValue) {
  76. this.destination.next(this._trailingValue);
  77. this._trailingValue = null;
  78. this._hasTrailingValue = false;
  79. }
  80. throttled.unsubscribe();
  81. this.remove(throttled);
  82. this.throttled = null;
  83. }
  84. };
  85. return ThrottleTimeSubscriber;
  86. }(Subscriber_1.Subscriber));
  87. function dispatchNext(arg) {
  88. var subscriber = arg.subscriber;
  89. subscriber.clearThrottle();
  90. }
  91. //# sourceMappingURL=throttleTime.js.map