this repo has no description
at main 36 lines 1.4 kB view raw
1import { getActiveTransaction } from '@sentry/core'; 2import { logger } from '@sentry/utils'; 3import { WINDOW } from './types.js'; 4 5/** 6 * Add a listener that cancels and finishes a transaction when the global 7 * document is hidden. 8 */ 9function registerBackgroundTabDetection() { 10 if (WINDOW && WINDOW.document) { 11 WINDOW.document.addEventListener('visibilitychange', () => { 12 const activeTransaction = getActiveTransaction() ; 13 if (WINDOW.document.hidden && activeTransaction) { 14 const statusType = 'cancelled'; 15 16 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && 17 logger.log( 18 `[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${activeTransaction.op}`, 19 ); 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 (!activeTransaction.status) { 23 activeTransaction.setStatus(statusType); 24 } 25 activeTransaction.setTag('visibilitychange', 'document.hidden'); 26 activeTransaction.finish(); 27 } 28 }); 29 } else { 30 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && 31 logger.warn('[Tracing] Could not set up background tab detection due to lack of global document'); 32 } 33} 34 35export { registerBackgroundTabDetection }; 36//# sourceMappingURL=backgroundtab.js.map