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;