debounce.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { operate } from '../util/lift';
  2. import { noop } from '../util/noop';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. import { innerFrom } from '../observable/innerFrom';
  5. export function debounce(durationSelector) {
  6. return operate((source, subscriber) => {
  7. let hasValue = false;
  8. let lastValue = null;
  9. let durationSubscriber = null;
  10. const emit = () => {
  11. durationSubscriber === null || durationSubscriber === void 0 ? void 0 : durationSubscriber.unsubscribe();
  12. durationSubscriber = null;
  13. if (hasValue) {
  14. hasValue = false;
  15. const value = lastValue;
  16. lastValue = null;
  17. subscriber.next(value);
  18. }
  19. };
  20. source.subscribe(createOperatorSubscriber(subscriber, (value) => {
  21. durationSubscriber === null || durationSubscriber === void 0 ? void 0 : durationSubscriber.unsubscribe();
  22. hasValue = true;
  23. lastValue = value;
  24. durationSubscriber = createOperatorSubscriber(subscriber, emit, noop);
  25. innerFrom(durationSelector(value)).subscribe(durationSubscriber);
  26. }, () => {
  27. emit();
  28. subscriber.complete();
  29. }, undefined, () => {
  30. lastValue = durationSubscriber = null;
  31. }));
  32. });
  33. }
  34. //# sourceMappingURL=debounce.js.map