1
1
/* eslint-disable max-lines */ // TODO: We might want to split this file up
2
+ import { WINDOW } from '@sentry/browser' ;
2
3
import { addGlobalEventProcessor , getCurrentHub , Scope , setContext } from '@sentry/core' ;
3
4
import { Breadcrumb , Client , Event , Integration } from '@sentry/types' ;
4
5
import { addInstrumentationHandler , createEnvelope , logger } from '@sentry/utils' ;
@@ -38,6 +39,7 @@ import { captureInternalException } from './util/captureInternalException';
38
39
import { createBreadcrumb } from './util/createBreadcrumb' ;
39
40
import { createPayload } from './util/createPayload' ;
40
41
import { dedupePerformanceEntries } from './util/dedupePerformanceEntries' ;
42
+ import { isBrowser } from './util/isBrowser' ;
41
43
import { isExpired } from './util/isExpired' ;
42
44
import { isSessionExpired } from './util/isSessionExpired' ;
43
45
@@ -53,8 +55,6 @@ const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed
53
55
54
56
let _initialized = false ;
55
57
56
- const isBrowser = typeof window !== 'undefined' ;
57
-
58
58
export class Replay implements Integration {
59
59
/**
60
60
* @inheritDoc
@@ -210,7 +210,7 @@ export class Replay implements Integration {
210
210
maxWait : this . options . flushMaxDelay ,
211
211
} ) ;
212
212
213
- if ( isBrowser && _initialized ) {
213
+ if ( isBrowser ( ) && _initialized ) {
214
214
const error = new Error ( 'Multiple Sentry Session Replay instances are not supported' ) ;
215
215
captureInternalException ( error ) ;
216
216
throw error ;
@@ -229,11 +229,11 @@ export class Replay implements Integration {
229
229
* here to avoid any future issues.
230
230
*/
231
231
setupOnce ( ) : void {
232
- if ( ! isBrowser ) {
232
+ if ( ! isBrowser ( ) ) {
233
233
return ;
234
234
}
235
235
// XXX: See method comments above
236
- window . setTimeout ( ( ) => this . start ( ) ) ;
236
+ setTimeout ( ( ) => this . start ( ) ) ;
237
237
}
238
238
239
239
/**
@@ -243,7 +243,7 @@ export class Replay implements Integration {
243
243
* PerformanceObserver, Recording, Sentry SDK, etc)
244
244
*/
245
245
start ( ) : void {
246
- if ( ! isBrowser ) {
246
+ if ( ! isBrowser ( ) ) {
247
247
return ;
248
248
}
249
249
@@ -309,7 +309,7 @@ export class Replay implements Integration {
309
309
* does not support a teardown
310
310
*/
311
311
stop ( ) : void {
312
- if ( ! isBrowser ) {
312
+ if ( ! isBrowser ( ) ) {
313
313
return ;
314
314
}
315
315
@@ -397,8 +397,8 @@ export class Replay implements Integration {
397
397
* first flush.
398
398
*/
399
399
setInitialState ( ) : void {
400
- const urlPath = `${ window . location . pathname } ${ window . location . hash } ${ window . location . search } ` ;
401
- const url = `${ window . location . origin } ${ urlPath } ` ;
400
+ const urlPath = `${ WINDOW . location . pathname } ${ WINDOW . location . hash } ${ WINDOW . location . search } ` ;
401
+ const url = `${ WINDOW . location . origin } ${ urlPath } ` ;
402
402
403
403
this . performanceEvents = [ ] ;
404
404
@@ -415,9 +415,9 @@ export class Replay implements Integration {
415
415
*/
416
416
addListeners ( ) : void {
417
417
try {
418
- document . addEventListener ( 'visibilitychange' , this . handleVisibilityChange ) ;
419
- window . addEventListener ( 'blur' , this . handleWindowBlur ) ;
420
- window . addEventListener ( 'focus' , this . handleWindowFocus ) ;
418
+ WINDOW . document . addEventListener ( 'visibilitychange' , this . handleVisibilityChange ) ;
419
+ WINDOW . addEventListener ( 'blur' , this . handleWindowBlur ) ;
420
+ WINDOW . addEventListener ( 'focus' , this . handleWindowFocus ) ;
421
421
422
422
// There is no way to remove these listeners, so ensure they are only added once
423
423
if ( ! this . hasInitializedCoreListeners ) {
@@ -441,7 +441,7 @@ export class Replay implements Integration {
441
441
}
442
442
443
443
// PerformanceObserver //
444
- if ( ! ( 'PerformanceObserver' in window ) ) {
444
+ if ( ! ( 'PerformanceObserver' in WINDOW ) ) {
445
445
return ;
446
446
}
447
447
@@ -476,10 +476,10 @@ export class Replay implements Integration {
476
476
*/
477
477
removeListeners ( ) : void {
478
478
try {
479
- document . removeEventListener ( 'visibilitychange' , this . handleVisibilityChange ) ;
479
+ WINDOW . document . removeEventListener ( 'visibilitychange' , this . handleVisibilityChange ) ;
480
480
481
- window . removeEventListener ( 'blur' , this . handleWindowBlur ) ;
482
- window . removeEventListener ( 'focus' , this . handleWindowFocus ) ;
481
+ WINDOW . removeEventListener ( 'blur' , this . handleWindowBlur ) ;
482
+ WINDOW . removeEventListener ( 'focus' , this . handleWindowFocus ) ;
483
483
484
484
if ( this . performanceObserver ) {
485
485
this . performanceObserver . disconnect ( ) ;
@@ -666,7 +666,7 @@ export class Replay implements Integration {
666
666
* page will also trigger a change to a hidden state.
667
667
*/
668
668
handleVisibilityChange : ( ) => void = ( ) => {
669
- if ( document . visibilityState === 'visible' ) {
669
+ if ( WINDOW . document . visibilityState === 'visible' ) {
670
670
this . doChangeToForegroundTasks ( ) ;
671
671
} else {
672
672
this . doChangeToBackgroundTasks ( ) ;
@@ -981,13 +981,13 @@ export class Replay implements Integration {
981
981
addMemoryEntry ( ) : Promise < void [ ] > | undefined {
982
982
// window.performance.memory is a non-standard API and doesn't work on all browsers
983
983
// so we check before creating the event.
984
- if ( ! ( 'memory' in window . performance ) ) {
984
+ if ( ! ( 'memory' in WINDOW . performance ) ) {
985
985
return ;
986
986
}
987
987
988
988
return this . createPerformanceSpans ( [
989
989
// @ts -ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
990
- createMemoryEntry ( window . performance . memory ) ,
990
+ createMemoryEntry ( WINDOW . performance . memory ) ,
991
991
] ) ;
992
992
}
993
993
0 commit comments