@@ -524,6 +524,7 @@ export class ReplayContainer implements ReplayContainerInterface {
524
524
}
525
525
526
526
const activityTime = Date . now ( ) ;
527
+ const earliestEvent = this . eventBuffer && this . eventBuffer . getEarliestTimestamp ( ) ;
527
528
528
529
DEBUG_BUILD && logger . info ( 'Converting buffer to session' ) ;
529
530
@@ -549,6 +550,9 @@ export class ReplayContainer implements ReplayContainerInterface {
549
550
550
551
// Once this session ends, we do not want to refresh it
551
552
if ( this . session ) {
553
+ if ( earliestEvent ) {
554
+ this . session . started = earliestEvent ;
555
+ }
552
556
this . _updateUserActivity ( activityTime ) ;
553
557
this . _updateSessionActivity ( activityTime ) ;
554
558
this . _maybeSaveSession ( ) ;
@@ -1222,35 +1226,14 @@ export class ReplayContainer implements ReplayContainerInterface {
1222
1226
return ;
1223
1227
}
1224
1228
1225
- const start = this . session . started ;
1226
- const now = Date . now ( ) ;
1227
- const duration = now - start ;
1228
-
1229
1229
// A flush is about to happen, cancel any queued flushes
1230
1230
this . _debouncedFlush . cancel ( ) ;
1231
1231
1232
- // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
1233
- // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
1234
- const tooShort = duration < this . _options . minReplayDuration ;
1235
- const tooLong = duration > this . _options . maxReplayDuration + 5_000 ;
1236
- if ( tooShort || tooLong ) {
1237
- DEBUG_BUILD &&
1238
- logger . info (
1239
- `Session duration (${ Math . floor ( duration / 1000 ) } s) is too ${
1240
- tooShort ? 'short' : 'long'
1241
- } , not sending replay.`,
1242
- ) ;
1243
-
1244
- if ( tooShort ) {
1245
- this . _debouncedFlush ( ) ;
1246
- }
1232
+ const isValidDuration = this . _checkReplayDurationDuringFlush ( ) ;
1247
1233
1248
- // XXX: disregard durations for buffer mode for debug purposes
1249
- if ( this . recordingMode !== 'buffer' ) {
1250
- return ;
1251
- } else {
1252
- setTag ( `replay.${ tooShort ? 'tooShort' : 'tooLong' } ` , true ) ;
1253
- }
1234
+ // XXX: disregard durations for buffer mode for debug purposes
1235
+ if ( ! isValidDuration && this . recordingMode !== 'buffer' ) {
1236
+ return ;
1254
1237
}
1255
1238
1256
1239
const eventBuffer = this . eventBuffer ;
@@ -1285,6 +1268,52 @@ export class ReplayContainer implements ReplayContainerInterface {
1285
1268
}
1286
1269
} ;
1287
1270
1271
+ /**
1272
+ * Checks to see if replay duration is within bounds during a flush. If it is
1273
+ * too short, will queue up a new flush to prevent short replays.
1274
+ *
1275
+ * Returns true if duration is ok, false otherwise
1276
+ */
1277
+ private _checkReplayDurationDuringFlush ( ) : boolean {
1278
+ if ( ! this . session ) {
1279
+ return false ;
1280
+ }
1281
+
1282
+ const earliestTimestampFromBuffer = this . eventBuffer && this . eventBuffer . getEarliestTimestamp ( ) ;
1283
+ const start =
1284
+ this . recordingMode === 'buffer' && earliestTimestampFromBuffer
1285
+ ? earliestTimestampFromBuffer
1286
+ : this . session . started ;
1287
+ const now = Date . now ( ) ;
1288
+ const duration = now - start ;
1289
+
1290
+ // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
1291
+ // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
1292
+ const tooShort = duration < this . _options . minReplayDuration ;
1293
+ const tooLong = duration > this . _options . maxReplayDuration + 5_000 ;
1294
+ if ( tooShort || tooLong ) {
1295
+ DEBUG_BUILD &&
1296
+ logger . info (
1297
+ `Session duration (${ Math . floor ( duration / 1000 ) } s) is too ${
1298
+ tooShort ? 'short' : 'long'
1299
+ } , not sending replay.`,
1300
+ ) ;
1301
+
1302
+ if ( tooShort ) {
1303
+ this . _debouncedFlush ( ) ;
1304
+ }
1305
+
1306
+ // XXX: disregard durations for buffer mode for debug purposes
1307
+ if ( this . recordingMode === 'buffer' ) {
1308
+ setTag ( `replay.${ tooShort ? 'tooShort' : 'tooLong' } ` , true ) ;
1309
+ }
1310
+
1311
+ return false ;
1312
+ }
1313
+
1314
+ return true ;
1315
+ }
1316
+
1288
1317
/** Save the session, if it is sticky */
1289
1318
private _maybeSaveSession ( ) : void {
1290
1319
if ( this . session && this . _options . stickySession ) {
0 commit comments