Skip to content

Commit

Permalink
full snapshots 5 minutes apart
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Jan 27, 2024
1 parent 5a13970 commit 4a983eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1484,4 +1484,25 @@ describe('SessionRecording', () => {
expect(sessionRecording['rrwebRecord']).not.toBeUndefined()
})
})

describe('scheduled full snapshots', () => {
it('starts out unscheduled', () => {
expect(sessionRecording['_fullSnapshotTimer']).toBe(undefined)
})

it('schedules a snapshot on start', () => {
sessionRecording.startRecordingIfEnabled()
expect(sessionRecording['_fullSnapshotTimer']).not.toBe(undefined)
})

it('reschedules a snapshot, when we take a full snapshot', () => {
sessionRecording.startRecordingIfEnabled()
const startTimer = sessionRecording['_fullSnapshotTimer']

_emit(createFullSnapshot())

expect(sessionRecording['_fullSnapshotTimer']).not.toBe(undefined)
expect(sessionRecording['_fullSnapshotTimer']).not.toBe(startTimer)
})
})
})
22 changes: 22 additions & 0 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export class SessionRecording {
private _canvasFps: number | null = null
private _canvasQuality: number | null = null

private _fullSnapshotTimer?: number

// Util to help developers working on this feature manually override
_forceAllowLocalhostNetworkCapture = false

Expand Down Expand Up @@ -575,6 +577,11 @@ export class SessionRecording {
},
})

// rrweb takes a snapshot on initialization,
// but we want to take one in five minutes
// if nothing else happens to reset the timer
this._scheduleFullSnapshot()

const activePlugins = this._gatherRRWebPlugins()
this.stopRrweb = this.rrwebRecord({
emit: (event) => {
Expand Down Expand Up @@ -612,6 +619,16 @@ export class SessionRecording {
})
}

private _scheduleFullSnapshot(): void {
if (this._fullSnapshotTimer) {
clearInterval(this._fullSnapshotTimer)
}

this._fullSnapshotTimer = setInterval(() => {
this._tryTakeFullSnapshot()
}, 1000 * 60 * 5) // 5 minutes
}

private _gatherRRWebPlugins() {
const plugins: RecordPlugin<unknown>[] = []

Expand Down Expand Up @@ -651,6 +668,11 @@ export class SessionRecording {
rawEvent.data.href = href
}

if (rawEvent.type === EventType.FullSnapshot) {
// we're processing a full snapshot, so we should reset the timer
this._scheduleFullSnapshot()
}

const throttledEvent = this.mutationRateLimiter
? this.mutationRateLimiter.throttleMutations(rawEvent)
: rawEvent
Expand Down

0 comments on commit 4a983eb

Please sign in to comment.