Skip to content

Commit

Permalink
fix: safer custom event on return from idle (#913)
Browse files Browse the repository at this point in the history
* fix: safer custom event on return from idle

* Revert "fix: safer custom event on return from idle"

This reverts commit 39d1d09.

* just swallow errors the same way

* split similar and different
  • Loading branch information
pauldambra authored Nov 28, 2023
1 parent 2da0d17 commit 6990c1e
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export class SessionRecording {
// We check if the lastActivityTimestamp is old enough to go idle
if (event.timestamp - this._lastActivityTimestamp > RECORDING_IDLE_ACTIVITY_TIMEOUT_MS) {
this.isIdle = true
this.rrwebRecord?.addCustomEvent('sessionIdle', {
this._tryAddCustomEvent('sessionIdle', {
reason: 'user inactivity',
timeSinceLastActive: event.timestamp - this._lastActivityTimestamp,
threshold: RECORDING_IDLE_ACTIVITY_TIMEOUT_MS,
Expand All @@ -402,7 +402,7 @@ export class SessionRecording {
if (this.isIdle) {
// Remove the idle state if set and trigger a full snapshot as we will have ignored previous mutations
this.isIdle = false
this.rrwebRecord?.addCustomEvent('sessionNoLongerIdle', {
this._tryAddCustomEvent('sessionNoLongerIdle', {
reason: 'user activity',
type: event.type,
})
Expand Down Expand Up @@ -434,21 +434,28 @@ export class SessionRecording {
this.sessionId = sessionId
}

private _tryTakeFullSnapshot(): boolean {
// TODO this should ignore based on emit?
private _tryRRwebMethod(rrwebMethod: () => void): boolean {
if (!this._captureStarted) {
return false
}
try {
this.rrwebRecord?.takeFullSnapshot()
rrwebMethod()
return true
} catch (e) {
// Sometimes a race can occur where the recorder is not fully started yet, so we can't take a full snapshot.
logger.error('Error taking full snapshot.', e)
// Sometimes a race can occur where the recorder is not fully started yet
logger.error('[Session-Recording] using rrweb when not started.', e)
return false
}
}

private _tryAddCustomEvent(tag: string, payload: any): boolean {
return this._tryRRwebMethod(() => this.rrwebRecord?.addCustomEvent(tag, payload))
}

private _tryTakeFullSnapshot(): boolean {
return this._tryRRwebMethod(() => this.rrwebRecord?.takeFullSnapshot())
}

private _onScriptLoaded() {
// rrweb config info: https://github.com/rrweb-io/rrweb/blob/7d5d0033258d6c29599fb08412202d9a2c7b9413/src/record/index.ts#L28
const sessionRecordingOptions: recordOptions<eventWithTime> = {
Expand Down Expand Up @@ -541,7 +548,7 @@ export class SessionRecording {
if (!href) {
return
}
this.rrwebRecord?.addCustomEvent('$pageview', { href })
this._tryAddCustomEvent('$pageview', { href })
}
} catch (e) {
logger.error('Could not add $pageview to rrweb session', e)
Expand Down

0 comments on commit 6990c1e

Please sign in to comment.