Skip to content

Commit 7d71794

Browse files
committed
ref(replay): Improve handling of checkout events
1 parent b6a4198 commit 7d71794

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

packages/replay/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ export interface EventBuffer {
271271

272272
/**
273273
* Add an event to the event buffer.
274+
* `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
274275
*
275276
* Returns a promise that resolves if the event was successfully added, else rejects.
276277
*/

packages/replay/src/util/addEvent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { SESSION_IDLE_DURATION } from '../constants';
55
import type { AddEventResult, RecordingEvent, ReplayContainer } from '../types';
66

77
/**
8-
* Add an event to the event buffer
8+
* Add an event to the event buffer.
9+
* `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
910
*/
1011
export async function addEvent(
1112
replay: ReplayContainer,

packages/replay/src/util/handleRecordingEmit.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@ type RecordingEmitCallback = (event: RecordingEvent, isCheckout?: boolean) => vo
1313
* Adds to event buffer, and has varying flushing behaviors if the event was a checkout.
1414
*/
1515
export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCallback {
16-
return (event: RecordingEvent, isCheckout?: boolean) => {
16+
let hadFirstEvent = false;
17+
18+
return (event: RecordingEvent, _isCheckout?: boolean) => {
1719
// If this is false, it means session is expired, create and a new session and wait for checkout
1820
if (!replay.checkAndHandleExpiredSession()) {
1921
__DEBUG_BUILD__ && logger.error('[Replay] Received replay event after session expired.');
2022

2123
return;
2224
}
2325

26+
// `_isCheckout` is only set when the checkout is due to `checkoutEveryNms`
27+
// We also want to treat the first event as a checkout, so we handle this specifically here
28+
const isCheckout = _isCheckout || !hadFirstEvent;
29+
hadFirstEvent = true;
30+
2431
replay.addUpdate(() => {
2532
// The session is always started immediately on pageload/init, but for
2633
// error-only replays, it should reflect the most recent checkout
2734
// when an error occurs. Clear any state that happens before this current
2835
// checkout. This needs to happen before `addEvent()` which updates state
2936
// dependent on this reset.
30-
if (replay.recordingMode === 'error' && event.type === 2) {
37+
if (replay.recordingMode === 'error' && isCheckout) {
3138
replay.setInitialState();
3239
}
3340

@@ -37,7 +44,7 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
3744

3845
// Different behavior for full snapshots (type=2), ignore other event types
3946
// See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16
40-
if (event.type !== 2) {
47+
if (!isCheckout) {
4148
return false;
4249
}
4350

0 commit comments

Comments
 (0)