Skip to content
Open
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
19 changes: 13 additions & 6 deletions src/modules/session-recorder/SessionRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class SessionRecorder {
#url: string;
#environment: Environment;
#lastTwoSessionEvents: [RQSessionEvents, RQSessionEvents];
#checkoutIntervalID: NodeJS.Timeout;

constructor(options: SessionRecorderOptions) {
this.#options = {
Expand Down Expand Up @@ -107,17 +108,13 @@ export class SessionRecorder {
this.#stopRecording = record({
plugins,
...commonRrwebOptions,
checkoutEveryNms: this.#options.maxDuration,
emit: (event, isCheckout) => {
if (isCheckout) {
const previousSessionEvents = this.#lastTwoSessionEvents[1];
const previousSessionRRWebEvents = previousSessionEvents[exports.RQSessionEventType.RRWEB];
const previousSessionRRWebEvents = previousSessionEvents[RQSessionEventType.RRWEB];
if (previousSessionRRWebEvents.length > 1) {
const timeElapsedInBucket =
previousSessionRRWebEvents[previousSessionRRWebEvents.length - 1].timestamp -
previousSessionRRWebEvents[0].timestamp;
// final session duration should be between T and 2T where T is maxDuration
if (timeElapsedInBucket >= this.#options.maxDuration) {
if (event.type === EventType.Meta) {
this.#lastTwoSessionEvents = [previousSessionEvents, this.#getEmptySessionEvents()];
}
}
Expand All @@ -127,14 +124,24 @@ export class SessionRecorder {
},
});

this.#checkoutIntervalID = setInterval(() => {
this.#takeSnapshot();
}, this.#options.maxDuration);

this.#interceptNetworkRequests((event) => {
this.#addEvent(RQSessionEventType.NETWORK, event);
});
}

#takeSnapshot(): void {
record.takeFullSnapshot(true);
}

stop(): void {
this.#stopRecording?.();
this.#stopRecording = null;
this.#checkoutIntervalID && clearInterval(this.#checkoutIntervalID);
this.#checkoutIntervalID = null;
Network.clearInterceptors();
}

Expand Down