debounceTime.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /** PURE_IMPORTS_START tslib,_Subscriber,_scheduler_async PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. import { async } from '../scheduler/async';
  5. export function debounceTime(dueTime, scheduler) {
  6. if (scheduler === void 0) {
  7. scheduler = async;
  8. }
  9. return function (source) { return source.lift(new DebounceTimeOperator(dueTime, scheduler)); };
  10. }
  11. var DebounceTimeOperator = /*@__PURE__*/ (function () {
  12. function DebounceTimeOperator(dueTime, scheduler) {
  13. this.dueTime = dueTime;
  14. this.scheduler = scheduler;
  15. }
  16. DebounceTimeOperator.prototype.call = function (subscriber, source) {
  17. return source.subscribe(new DebounceTimeSubscriber(subscriber, this.dueTime, this.scheduler));
  18. };
  19. return DebounceTimeOperator;
  20. }());
  21. var DebounceTimeSubscriber = /*@__PURE__*/ (function (_super) {
  22. tslib_1.__extends(DebounceTimeSubscriber, _super);
  23. function DebounceTimeSubscriber(destination, dueTime, scheduler) {
  24. var _this = _super.call(this, destination) || this;
  25. _this.dueTime = dueTime;
  26. _this.scheduler = scheduler;
  27. _this.debouncedSubscription = null;
  28. _this.lastValue = null;
  29. _this.hasValue = false;
  30. return _this;
  31. }
  32. DebounceTimeSubscriber.prototype._next = function (value) {
  33. this.clearDebounce();
  34. this.lastValue = value;
  35. this.hasValue = true;
  36. this.add(this.debouncedSubscription = this.scheduler.schedule(dispatchNext, this.dueTime, this));
  37. };
  38. DebounceTimeSubscriber.prototype._complete = function () {
  39. this.debouncedNext();
  40. this.destination.complete();
  41. };
  42. DebounceTimeSubscriber.prototype.debouncedNext = function () {
  43. this.clearDebounce();
  44. if (this.hasValue) {
  45. var lastValue = this.lastValue;
  46. this.lastValue = null;
  47. this.hasValue = false;
  48. this.destination.next(lastValue);
  49. }
  50. };
  51. DebounceTimeSubscriber.prototype.clearDebounce = function () {
  52. var debouncedSubscription = this.debouncedSubscription;
  53. if (debouncedSubscription !== null) {
  54. this.remove(debouncedSubscription);
  55. debouncedSubscription.unsubscribe();
  56. this.debouncedSubscription = null;
  57. }
  58. };
  59. return DebounceTimeSubscriber;
  60. }(Subscriber));
  61. function dispatchNext(subscriber) {
  62. subscriber.debouncedNext();
  63. }
  64. //# sourceMappingURL=debounceTime.js.map