Skip to content

Commit

Permalink
fix: event listeners should obey start and stop (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Aug 26, 2024
1 parent 10aa516 commit f7cc004
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,6 @@ export class SessionRecording {
this.stopRrweb = undefined
this.receivedDecide = false

window?.addEventListener('beforeunload', () => {
this._flushBuffer()
})

window?.addEventListener('offline', () => {
this._tryAddCustomEvent('browser offline', {})
})

window?.addEventListener('online', () => {
this._tryAddCustomEvent('browser online', {})
})

window?.addEventListener('visibilitychange', () => {
if (document?.visibilityState) {
const label = 'window ' + document.visibilityState
this._tryAddCustomEvent(label, {})
}
})

if (!this.instance.sessionManager) {
logger.error(LOGGER_PREFIX + ' started without valid sessionManager')
throw new Error(LOGGER_PREFIX + ' started without valid sessionManager. This is a bug.')
Expand All @@ -280,9 +261,34 @@ export class SessionRecording {
this._setupSampling()
}

private _onBeforeUnload = (): void => {
this._flushBuffer()
}

private _onOffline = (): void => {
this._tryAddCustomEvent('browser offline', {})
}

private _onOnline = (): void => {
this._tryAddCustomEvent('browser online', {})
}

private _onVisibilityChange = (): void => {
if (document?.visibilityState) {
const label = 'window ' + document.visibilityState
this._tryAddCustomEvent(label, {})
}
}

startIfEnabledOrStop() {
if (this.isRecordingEnabled) {
this._startCapture()

window?.addEventListener('beforeunload', this._onBeforeUnload)
window?.addEventListener('offline', this._onOffline)
window?.addEventListener('online', this._onOnline)
window?.addEventListener('visibilitychange', this._onVisibilityChange)

logger.info(LOGGER_PREFIX + ' started')
} else {
this.stopRecording()
Expand All @@ -295,6 +301,12 @@ export class SessionRecording {
this.stopRrweb()
this.stopRrweb = undefined
this._captureStarted = false

window?.removeEventListener('beforeunload', this._onBeforeUnload)
window?.removeEventListener('offline', this._onOffline)
window?.removeEventListener('online', this._onOnline)
window?.removeEventListener('visibilitychange', this._onVisibilityChange)

logger.info(LOGGER_PREFIX + ' stopped')
}
}
Expand Down

0 comments on commit f7cc004

Please sign in to comment.