Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a little session buffering logic #890

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,31 @@ describe('SessionRecording', () => {
expect(posthog.capture).not.toHaveBeenCalled()
})

it('does flush if session duration is negative', () => {
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { minimumDurationMilliseconds: 1500 },
})
)
sessionRecording.startRecordingIfEnabled()
expect(sessionRecording['status']).toBe('active')
const { sessionStartTimestamp } = sessionManager.checkAndGetSessionAndWindowId(true)

// if we have some data in the buffer and the buffer has a session id but then the session id changes
// then the session duration will be negative, and we will never flush the buffer
// this setup isn't quite that but does simulate the behaviour closely enough
_emit(createIncrementalSnapshot({ data: { source: 1 }, timestamp: sessionStartTimestamp - 1000 }))

expect(sessionRecording['sessionDuration']).toBe(-1000)
expect(sessionRecording['_minimumDuration']).toBe(1500)

expect(sessionRecording['buffer']?.data.length).toBe(1)
// call the private method to avoid waiting for the timer
sessionRecording['_flushBuffer']()

expect(posthog.capture).toHaveBeenCalled()
})

it('does not stay buffering after the minimum duration', () => {
sessionRecording.afterDecideResponse(
makeDecideResponse({
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,11 @@ export class SessionRecording {

const minimumDuration = this._minimumDuration
const sessionDuration = this.sessionDuration
// if we have old data in the buffer but the session has rotated then the
// session duration might be negative, in that case we want to flush the buffer
const isPositiveSessionDuration = _isNumber(sessionDuration) && sessionDuration >= 0
const isBelowMinimumDuration =
_isNumber(minimumDuration) && _isNumber(sessionDuration) && sessionDuration < minimumDuration
_isNumber(minimumDuration) && isPositiveSessionDuration && sessionDuration < minimumDuration

if (this.status === 'buffering' || isBelowMinimumDuration) {
this.flushBufferTimer = setTimeout(() => {
Expand Down
Loading