- 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,
|
* If the array have multiple meta and fullsnapshot events,
|
||||||
* return the events from last meta to the end.
|
* return the events from last meta to the end.
|
||||||
*/
|
*/
|
||||||
export function getLastSession(
|
export function discardPriorSnapshots(
|
||||||
events: eventWithTime[],
|
events: eventWithTime[],
|
||||||
baselineTime: number,
|
baselineTime: number,
|
||||||
): eventWithTime[] {
|
): eventWithTime[] {
|
||||||
let startMetaIdx: number | null = null;
|
|
||||||
let endMetaIdx: number | null = null;
|
|
||||||
|
|
||||||
for (let idx = events.length - 1; idx >= 0; idx--) {
|
for (let idx = events.length - 1; idx >= 0; idx--) {
|
||||||
const event = events[idx];
|
const event = events[idx];
|
||||||
if (event.type === EventType.Meta) {
|
if (event.type === EventType.Meta) {
|
||||||
if (event.timestamp > baselineTime) {
|
if (event.timestamp <= baselineTime) {
|
||||||
endMetaIdx = idx;
|
return events.slice(idx);
|
||||||
} else {
|
|
||||||
startMetaIdx = idx;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (startMetaIdx !== null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return events;
|
||||||
// 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,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlayerAssets = {
|
type PlayerAssets = {
|
||||||
@@ -208,7 +190,7 @@ export function createPlayerService(
|
|||||||
play(ctx) {
|
play(ctx) {
|
||||||
const { timer, events, baselineTime, lastPlayedEvent } = ctx;
|
const { timer, events, baselineTime, lastPlayedEvent } = ctx;
|
||||||
timer.clear();
|
timer.clear();
|
||||||
const neededEvents = getLastSession(events, baselineTime);
|
const neededEvents = discardPriorSnapshots(events, baselineTime);
|
||||||
|
|
||||||
const actions = new Array<actionWithDelay>();
|
const actions = new Array<actionWithDelay>();
|
||||||
for (const event of neededEvents) {
|
for (const event of neededEvents) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { getLastSession } from '../src/replay/machine';
|
import { discardPriorSnapshots } from '../src/replay/machine';
|
||||||
import { sampleEvents } from './utils';
|
import { sampleEvents } from './utils';
|
||||||
import { EventType } from '../src/types';
|
import { EventType } from '../src/types';
|
||||||
|
|
||||||
@@ -17,13 +17,13 @@ const nextNextEvents = nextEvents.map((e) => ({
|
|||||||
|
|
||||||
describe('get last session', () => {
|
describe('get last session', () => {
|
||||||
it('will return all the events when there is only one 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', () => {
|
it('will return last session when there is more than one in the events', () => {
|
||||||
const multiple = events.concat(nextEvents).concat(nextNextEvents);
|
const multiple = events.concat(nextEvents).concat(nextNextEvents);
|
||||||
expect(
|
expect(
|
||||||
getLastSession(
|
discardPriorSnapshots(
|
||||||
multiple,
|
multiple,
|
||||||
nextNextEvents[nextNextEvents.length - 1].timestamp,
|
nextNextEvents[nextNextEvents.length - 1].timestamp,
|
||||||
),
|
),
|
||||||
@@ -33,15 +33,15 @@ describe('get last session', () => {
|
|||||||
it('will return last session when baseline time is future time', () => {
|
it('will return last session when baseline time is future time', () => {
|
||||||
const multiple = events.concat(nextEvents).concat(nextNextEvents);
|
const multiple = events.concat(nextEvents).concat(nextNextEvents);
|
||||||
expect(
|
expect(
|
||||||
getLastSession(
|
discardPriorSnapshots(
|
||||||
multiple,
|
multiple,
|
||||||
nextNextEvents[nextNextEvents.length - 1].timestamp + 1000,
|
nextNextEvents[nextNextEvents.length - 1].timestamp + 1000,
|
||||||
),
|
),
|
||||||
).to.deep.equal(nextNextEvents);
|
).to.deep.equal(nextNextEvents);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('will return first session when baseline time is previous time', () => {
|
it('will return all sessions when baseline time is prior time', () => {
|
||||||
expect(getLastSession(events, events[0].timestamp - 1000)).to.deep.equal(
|
expect(discardPriorSnapshots(events, events[0].timestamp - 1000)).to.deep.equal(
|
||||||
events,
|
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';
|
value: 'live';
|
||||||
context: PlayerContext;
|
context: PlayerContext;
|
||||||
};
|
};
|
||||||
|
export declare function discardPriorSnapshots(events: eventWithTime[], baselineTime: number): eventWithTime[];
|
||||||
declare type PlayerAssets = {
|
declare type PlayerAssets = {
|
||||||
emitter: Emitter;
|
emitter: Emitter;
|
||||||
getCastFn(event: eventWithTime, isSync: boolean): () => void;
|
getCastFn(event: eventWithTime, isSync: boolean): () => void;
|
||||||
|
|||||||
Reference in New Issue
Block a user