audit.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { operate } from '../util/lift';
  2. import { innerFrom } from '../observable/innerFrom';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. export function audit(durationSelector) {
  5. return operate((source, subscriber) => {
  6. let hasValue = false;
  7. let lastValue = null;
  8. let durationSubscriber = null;
  9. let isComplete = false;
  10. const endDuration = () => {
  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. isComplete && subscriber.complete();
  20. };
  21. const cleanupDuration = () => {
  22. durationSubscriber = null;
  23. isComplete && subscriber.complete();
  24. };
  25. source.subscribe(createOperatorSubscriber(subscriber, (value) => {
  26. hasValue = true;
  27. lastValue = value;
  28. if (!durationSubscriber) {
  29. innerFrom(durationSelector(value)).subscribe((durationSubscriber = createOperatorSubscriber(subscriber, endDuration, cleanupDuration)));
  30. }
  31. }, () => {
  32. isComplete = true;
  33. (!hasValue || !durationSubscriber || durationSubscriber.closed) && subscriber.complete();
  34. }));
  35. });
  36. }
  37. //# sourceMappingURL=audit.js.map