From 5b0539419c9ae1dbfb70f9a7e7a56dbf029b9427 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] Restore functioning of #200 - this got broken after #242 (#246) - What was broken was that it would just play activity from the first page view, but then would stop at the second page view (meta) as actions after that had been discarded - This restores the functionality given by the comment 'return the events from last meta to the end.' - we never want to discard events that are after the baseline time - I believe 'session' is the incorrect terminology for this function name, as a session in web analytics usually means a series of page views --- src/replay/machine.ts | 28 +++++----------------------- test/machine.test.ts | 12 ++++++------ typings/replay/machine.d.ts | 1 + 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/src/replay/machine.ts b/src/replay/machine.ts index cdc794f9..74b944e4 100644 --- a/src/replay/machine.ts +++ b/src/replay/machine.ts @@ -79,37 +79,19 @@ export type PlayerState = * If the array have multiple meta and fullsnapshot events, * return the events from last meta to the end. */ -export function getLastSession( +export function discardPriorSnapshots( events: eventWithTime[], baselineTime: number, ): eventWithTime[] { - let startMetaIdx: number | null = null; - let endMetaIdx: number | null = null; - for (let idx = events.length - 1; idx >= 0; idx--) { const event = events[idx]; if (event.type === EventType.Meta) { - if (event.timestamp > baselineTime) { - endMetaIdx = idx; - } else { - startMetaIdx = idx; + if (event.timestamp <= baselineTime) { + return events.slice(idx); } } - if (startMetaIdx !== null) { - break; - } } - - // baseline time is less than first meta event - if (startMetaIdx === null && endMetaIdx !== null) { - startMetaIdx = endMetaIdx; - endMetaIdx = null; - } - - return events.slice( - startMetaIdx ?? 0, - endMetaIdx === null ? events.length : endMetaIdx + 1, - ); + return events; } type PlayerAssets = { @@ -208,7 +190,7 @@ export function createPlayerService( play(ctx) { const { timer, events, baselineTime, lastPlayedEvent } = ctx; timer.clear(); - const neededEvents = getLastSession(events, baselineTime); + const neededEvents = discardPriorSnapshots(events, baselineTime); const actions = new Array(); for (const event of neededEvents) { diff --git a/test/machine.test.ts b/test/machine.test.ts index 24a8179f..8e4e27fc 100644 --- a/test/machine.test.ts +++ b/test/machine.test.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { getLastSession } from '../src/replay/machine'; +import { discardPriorSnapshots } from '../src/replay/machine'; import { sampleEvents } from './utils'; import { EventType } from '../src/types'; @@ -17,13 +17,13 @@ const nextNextEvents = nextEvents.map((e) => ({ describe('get last session', () => { it('will return all the events when there is only one session', () => { - expect(getLastSession(events, events[0].timestamp)).to.deep.equal(events); + expect(discardPriorSnapshots(events, events[0].timestamp)).to.deep.equal(events); }); it('will return last session when there is more than one in the events', () => { const multiple = events.concat(nextEvents).concat(nextNextEvents); expect( - getLastSession( + discardPriorSnapshots( multiple, nextNextEvents[nextNextEvents.length - 1].timestamp, ), @@ -33,15 +33,15 @@ describe('get last session', () => { it('will return last session when baseline time is future time', () => { const multiple = events.concat(nextEvents).concat(nextNextEvents); expect( - getLastSession( + discardPriorSnapshots( multiple, nextNextEvents[nextNextEvents.length - 1].timestamp + 1000, ), ).to.deep.equal(nextNextEvents); }); - it('will return first session when baseline time is previous time', () => { - expect(getLastSession(events, events[0].timestamp - 1000)).to.deep.equal( + it('will return all sessions when baseline time is prior time', () => { + expect(discardPriorSnapshots(events, events[0].timestamp - 1000)).to.deep.equal( events, ); }); diff --git a/typings/replay/machine.d.ts b/typings/replay/machine.d.ts index 8c659e50..5eaff393 100644 --- a/typings/replay/machine.d.ts +++ b/typings/replay/machine.d.ts @@ -63,6 +63,7 @@ export declare type PlayerState = { value: 'live'; context: PlayerContext; }; +export declare function discardPriorSnapshots(events: eventWithTime[], baselineTime: number): eventWithTime[]; declare type PlayerAssets = { emitter: Emitter; getCastFn(event: eventWithTime, isSync: boolean): () => void;