- 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
This commit is contained in:
@@ -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<actionWithDelay>();
|
||||
for (const event of neededEvents) {
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
|
||||
1
typings/replay/machine.d.ts
vendored
1
typings/replay/machine.d.ts
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user