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.

debounce.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 OuterSubscriber_1 = require("../OuterSubscriber");
  17. var subscribeToResult_1 = require("../util/subscribeToResult");
  18. function debounce(durationSelector) {
  19. return function (source) { return source.lift(new DebounceOperator(durationSelector)); };
  20. }
  21. exports.debounce = debounce;
  22. var DebounceOperator = (function () {
  23. function DebounceOperator(durationSelector) {
  24. this.durationSelector = durationSelector;
  25. }
  26. DebounceOperator.prototype.call = function (subscriber, source) {
  27. return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
  28. };
  29. return DebounceOperator;
  30. }());
  31. var DebounceSubscriber = (function (_super) {
  32. __extends(DebounceSubscriber, _super);
  33. function DebounceSubscriber(destination, durationSelector) {
  34. var _this = _super.call(this, destination) || this;
  35. _this.durationSelector = durationSelector;
  36. _this.hasValue = false;
  37. _this.durationSubscription = null;
  38. return _this;
  39. }
  40. DebounceSubscriber.prototype._next = function (value) {
  41. try {
  42. var result = this.durationSelector.call(this, value);
  43. if (result) {
  44. this._tryNext(value, result);
  45. }
  46. }
  47. catch (err) {
  48. this.destination.error(err);
  49. }
  50. };
  51. DebounceSubscriber.prototype._complete = function () {
  52. this.emitValue();
  53. this.destination.complete();
  54. };
  55. DebounceSubscriber.prototype._tryNext = function (value, duration) {
  56. var subscription = this.durationSubscription;
  57. this.value = value;
  58. this.hasValue = true;
  59. if (subscription) {
  60. subscription.unsubscribe();
  61. this.remove(subscription);
  62. }
  63. subscription = subscribeToResult_1.subscribeToResult(this, duration);
  64. if (subscription && !subscription.closed) {
  65. this.add(this.durationSubscription = subscription);
  66. }
  67. };
  68. DebounceSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  69. this.emitValue();
  70. };
  71. DebounceSubscriber.prototype.notifyComplete = function () {
  72. this.emitValue();
  73. };
  74. DebounceSubscriber.prototype.emitValue = function () {
  75. if (this.hasValue) {
  76. var value = this.value;
  77. var subscription = this.durationSubscription;
  78. if (subscription) {
  79. this.durationSubscription = null;
  80. subscription.unsubscribe();
  81. this.remove(subscription);
  82. }
  83. this.value = null;
  84. this.hasValue = false;
  85. _super.prototype._next.call(this, value);
  86. }
  87. };
  88. return DebounceSubscriber;
  89. }(OuterSubscriber_1.OuterSubscriber));
  90. //# sourceMappingURL=debounce.js.map