From 6990c1eb2fb86d47a876a9d116290f25ed4a8c76 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Tue, 28 Nov 2023 17:38:11 +0000 Subject: [PATCH] fix: safer custom event on return from idle (#913) * fix: safer custom event on return from idle * Revert "fix: safer custom event on return from idle" This reverts commit 39d1d0960106b275a8ac3a933dfe3d65fcfa067b. * just swallow errors the same way * split similar and different --- src/extensions/replay/sessionrecording.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/extensions/replay/sessionrecording.ts b/src/extensions/replay/sessionrecording.ts index f8d770554..ab81b5553 100644 --- a/src/extensions/replay/sessionrecording.ts +++ b/src/extensions/replay/sessionrecording.ts @@ -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, @@ -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, }) @@ -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 = { @@ -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)