Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

debounce.js 2.1KB

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