From 2622651dc32bba3fd82754b1cd45efd8b90e7db2 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 8 Feb 2026 18:13:15 +0100 Subject: [PATCH] Fixed issues --- src/move.ts | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/move.ts b/src/move.ts index 9689abc..7e11248 100644 --- a/src/move.ts +++ b/src/move.ts @@ -6,6 +6,24 @@ export function startMonitoring(runId: string) { let moveId = 0; let moves: LiveRunMove[] = []; let lastLatency = Date.now(); + let idleTimeout: NodeJS.Timeout | undefined; + let isSending = false; + + const SEND_IDLE_MS = 350; + + const flushMoves = async () => { + if (!moves.length || isSending) return; + + isSending = true; + const toSend = moves; + moves = []; + + try { + await addRunMoves(runId, toSend); + } finally { + isSending = false; + } + }; const docSub = vscode.workspace.onDidChangeTextDocument((e) => { const editor = vscode.window.activeTextEditor; @@ -15,7 +33,7 @@ export function startMonitoring(runId: string) { if (!edit) return; const now = Date.now(); - const latency = capToZeroAbove(now - lastLatency, 350); + const latency = capToZeroAbove(now - lastLatency, SEND_IDLE_MS); lastLatency = now; const cursorOffset = editor.document.offsetAt(editor.selection.active); @@ -30,18 +48,20 @@ export function startMonitoring(runId: string) { }, moveId: moveId++, }); - }); - const sendInterval = setInterval(() => { - if (moves.length) { - addRunMoves(runId, moves); - moves = []; + if (idleTimeout) { + clearTimeout(idleTimeout); } - }, 3000); + + idleTimeout = setTimeout(() => { + flushMoves(); + }, SEND_IDLE_MS); + }); return () => { docSub.dispose(); - clearInterval(sendInterval); + if (idleTimeout) clearTimeout(idleTimeout); + flushMoves(); }; }