backgroundtab.js 1.7 KB

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