fix: VCR replay paginates — fetches next 10k when buffer exhausted instead of jumping to live

This commit is contained in:
you
2026-03-19 21:33:55 +00:00
parent 6dd258be65
commit ca96d5dfbc
+26 -1
View File
@@ -205,7 +205,11 @@
function tick() {
if (VCR.mode !== 'REPLAY') return;
if (VCR.playhead >= VCR.buffer.length) {
vcrResumeLive();
// Try to fetch the next page before going live
fetchNextReplayPage().then(hasMore => {
if (hasMore) tick();
else vcrResumeLive();
});
return;
}
const entry = VCR.buffer[VCR.playhead];
@@ -228,6 +232,27 @@
tick();
}
function fetchNextReplayPage() {
// Get timestamp of last packet in buffer to fetch the next page
const last = VCR.buffer[VCR.buffer.length - 1];
if (!last) return Promise.resolve(false);
const since = new Date(last.ts + 1).toISOString(); // +1ms to avoid dupe
return fetch(`/api/packets?limit=10000&grouped=false&since=${encodeURIComponent(since)}&order=asc`)
.then(r => r.json())
.then(data => {
const pkts = data.packets || [];
if (pkts.length === 0) return false;
const newEntries = pkts.map(p => ({
ts: new Date(p.timestamp || p.created_at).getTime(),
pkt: dbPacketToLive(p)
}));
// Append to buffer, playhead stays where it is (at the end, about to read new entries)
VCR.buffer = VCR.buffer.concat(newEntries);
return true;
})
.catch(() => false);
}
function stopReplay() {
if (VCR.replayTimer) { clearTimeout(VCR.replayTimer); VCR.replayTimer = null; }
}