diff --git a/src/modules/session-recorder/SessionRecorder.ts b/src/modules/session-recorder/SessionRecorder.ts index f5ad5cb..d686f49 100644 --- a/src/modules/session-recorder/SessionRecorder.ts +++ b/src/modules/session-recorder/SessionRecorder.ts @@ -39,6 +39,7 @@ export class SessionRecorder { #url: string; #environment: Environment; #lastTwoSessionEvents: [RQSessionEvents, RQSessionEvents]; + #checkoutIntervalID: NodeJS.Timeout; constructor(options: SessionRecorderOptions) { this.#options = { @@ -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()]; } } @@ -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(); }