forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackgroundtab.ts
35 lines (32 loc) · 1.33 KB
/
backgroundtab.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { logger, WINDOW } from '@sentry/utils';
import { IdleTransaction } from '../idletransaction';
import { SpanStatusType } from '../span';
import { getActiveTransaction } from '../utils';
/**
* Add a listener that cancels and finishes a transaction when the global
* document is hidden.
*/
export function registerBackgroundTabDetection(): void {
if (WINDOW && WINDOW.document) {
WINDOW.document.addEventListener('visibilitychange', () => {
const activeTransaction = getActiveTransaction() as IdleTransaction;
if (WINDOW.document.hidden && activeTransaction) {
const statusType: SpanStatusType = 'cancelled';
__DEBUG_BUILD__ &&
logger.log(
`[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${activeTransaction.op}`,
);
// We should not set status if it is already set, this prevent important statuses like
// error or data loss from being overwritten on transaction.
if (!activeTransaction.status) {
activeTransaction.setStatus(statusType);
}
activeTransaction.setTag('visibilitychange', 'document.hidden');
activeTransaction.finish();
}
});
} else {
__DEBUG_BUILD__ &&
logger.warn('[Tracing] Could not set up background tab detection due to lack of global document');
}
}