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.

debounceTime.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { Subscriber } from '../Subscriber';
  2. import { async } from '../scheduler/async';
  3. export function debounceTime(dueTime, scheduler = async) {
  4. return (source) => source.lift(new DebounceTimeOperator(dueTime, scheduler));
  5. }
  6. class DebounceTimeOperator {
  7. constructor(dueTime, scheduler) {
  8. this.dueTime = dueTime;
  9. this.scheduler = scheduler;
  10. }
  11. call(subscriber, source) {
  12. return source.subscribe(new DebounceTimeSubscriber(subscriber, this.dueTime, this.scheduler));
  13. }
  14. }
  15. class DebounceTimeSubscriber extends Subscriber {
  16. constructor(destination, dueTime, scheduler) {
  17. super(destination);
  18. this.dueTime = dueTime;
  19. this.scheduler = scheduler;
  20. this.debouncedSubscription = null;
  21. this.lastValue = null;
  22. this.hasValue = false;
  23. }
  24. _next(value) {
  25. this.clearDebounce();
  26. this.lastValue = value;
  27. this.hasValue = true;
  28. this.add(this.debouncedSubscription = this.scheduler.schedule(dispatchNext, this.dueTime, this));
  29. }
  30. _complete() {
  31. this.debouncedNext();
  32. this.destination.complete();
  33. }
  34. debouncedNext() {
  35. this.clearDebounce();
  36. if (this.hasValue) {
  37. const { lastValue } = this;
  38. this.lastValue = null;
  39. this.hasValue = false;
  40. this.destination.next(lastValue);
  41. }
  42. }
  43. clearDebounce() {
  44. const debouncedSubscription = this.debouncedSubscription;
  45. if (debouncedSubscription !== null) {
  46. this.remove(debouncedSubscription);
  47. debouncedSubscription.unsubscribe();
  48. this.debouncedSubscription = null;
  49. }
  50. }
  51. }
  52. function dispatchNext(subscriber) {
  53. subscriber.debouncedNext();
  54. }
  55. //# sourceMappingURL=debounceTime.js.map