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

View File

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