call sort after unpack

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent cfe59cb4b4
commit fdfb160e76
2 changed files with 12 additions and 13 deletions

View File

@@ -129,8 +129,6 @@ export class Replayer {
events: Array<eventWithTime | string>,
config?: Partial<playerConfig>,
) {
events.sort((a1, a2) => a1.timestamp - a2.timestamp);
if (!config?.liveMode && events.length < 2) {
throw new Error('Replayer need at least 2 events.');
}
@@ -199,12 +197,14 @@ export class Replayer {
const timer = new Timer([], config?.speed || defaultConfig.speed);
this.service = createPlayerService(
{
events: events.map((e) => {
if (config && config.unpackFn) {
return config.unpackFn(e as string);
}
return e as eventWithTime;
}),
events: events
.map((e) => {
if (config && config.unpackFn) {
return config.unpackFn(e as string);
}
return e as eventWithTime;
})
.sort((a1, a2) => a1.timestamp - a2.timestamp),
timer,
timeOffset: 0,
baselineTime: 0,

View File

@@ -243,7 +243,7 @@ export function createPlayerService(
// fast track
events.push(event);
} else {
let insertion_index = -1;
let insertionIndex = -1;
let start = 0;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
@@ -253,13 +253,12 @@ export function createPlayerService(
end = mid - 1;
}
}
if (insertion_index === -1) {
insertion_index = start;
if (insertionIndex === -1) {
insertionIndex = start;
}
events.splice(insertion_index, 0, event);
events.splice(insertionIndex, 0, event);
}
const isSync = event.timestamp < baselineTime;
const castFn = getCastFn(event, isSync);
if (isSync) {