debounce.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /** PURE_IMPORTS_START tslib,_innerSubscribe PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  4. export function debounce(durationSelector) {
  5. return function (source) { return source.lift(new DebounceOperator(durationSelector)); };
  6. }
  7. var DebounceOperator = /*@__PURE__*/ (function () {
  8. function DebounceOperator(durationSelector) {
  9. this.durationSelector = durationSelector;
  10. }
  11. DebounceOperator.prototype.call = function (subscriber, source) {
  12. return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
  13. };
  14. return DebounceOperator;
  15. }());
  16. var DebounceSubscriber = /*@__PURE__*/ (function (_super) {
  17. tslib_1.__extends(DebounceSubscriber, _super);
  18. function DebounceSubscriber(destination, durationSelector) {
  19. var _this = _super.call(this, destination) || this;
  20. _this.durationSelector = durationSelector;
  21. _this.hasValue = false;
  22. return _this;
  23. }
  24. DebounceSubscriber.prototype._next = function (value) {
  25. try {
  26. var result = this.durationSelector.call(this, value);
  27. if (result) {
  28. this._tryNext(value, result);
  29. }
  30. }
  31. catch (err) {
  32. this.destination.error(err);
  33. }
  34. };
  35. DebounceSubscriber.prototype._complete = function () {
  36. this.emitValue();
  37. this.destination.complete();
  38. };
  39. DebounceSubscriber.prototype._tryNext = function (value, duration) {
  40. var subscription = this.durationSubscription;
  41. this.value = value;
  42. this.hasValue = true;
  43. if (subscription) {
  44. subscription.unsubscribe();
  45. this.remove(subscription);
  46. }
  47. subscription = innerSubscribe(duration, new SimpleInnerSubscriber(this));
  48. if (subscription && !subscription.closed) {
  49. this.add(this.durationSubscription = subscription);
  50. }
  51. };
  52. DebounceSubscriber.prototype.notifyNext = function () {
  53. this.emitValue();
  54. };
  55. DebounceSubscriber.prototype.notifyComplete = function () {
  56. this.emitValue();
  57. };
  58. DebounceSubscriber.prototype.emitValue = function () {
  59. if (this.hasValue) {
  60. var value = this.value;
  61. var subscription = this.durationSubscription;
  62. if (subscription) {
  63. this.durationSubscription = undefined;
  64. subscription.unsubscribe();
  65. this.remove(subscription);
  66. }
  67. this.value = undefined;
  68. this.hasValue = false;
  69. _super.prototype._next.call(this, value);
  70. }
  71. };
  72. return DebounceSubscriber;
  73. }(SimpleOuterSubscriber));
  74. //# sourceMappingURL=debounce.js.map