backgroundtab.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { getActiveTransaction, spanToJSON } from '@sentry/core';
  2. import { logger } from '@sentry/utils';
  3. import { DEBUG_BUILD } from '../common/debug-build.js';
  4. import { WINDOW } from './types.js';
  5. /**
  6. * Add a listener that cancels and finishes a transaction when the global
  7. * document is hidden.
  8. */
  9. function registerBackgroundTabDetection() {
  10. if (WINDOW && WINDOW.document) {
  11. WINDOW.document.addEventListener('visibilitychange', () => {
  12. // eslint-disable-next-line deprecation/deprecation
  13. const activeTransaction = getActiveTransaction() ;
  14. if (WINDOW.document.hidden && activeTransaction) {
  15. const statusType = 'cancelled';
  16. const { op, status } = spanToJSON(activeTransaction);
  17. DEBUG_BUILD &&
  18. logger.log(`[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${op}`);
  19. // We should not set status if it is already set, this prevent important statuses like
  20. // error or data loss from being overwritten on transaction.
  21. if (!status) {
  22. activeTransaction.setStatus(statusType);
  23. }
  24. // TODO: Can we rewrite this to an attribute?
  25. // eslint-disable-next-line deprecation/deprecation
  26. activeTransaction.setTag('visibilitychange', 'document.hidden');
  27. activeTransaction.end();
  28. }
  29. });
  30. } else {
  31. DEBUG_BUILD && logger.warn('[Tracing] Could not set up background tab detection due to lack of global document');
  32. }
  33. }
  34. export { registerBackgroundTabDetection };
  35. //# sourceMappingURL=backgroundtab.js.map