Tab change

This commit is contained in:
2026-02-09 15:29:51 +01:00
parent 7d0ece5341
commit 666b7247b7
2 changed files with 53 additions and 11 deletions
+8 -1
View File
@@ -41,12 +41,19 @@ export async function submitRun(runId: string) {
);
}
export async function addRunMoves(runId: string, moves: LiveRunMove[]) {
export async function addRunMoves(
runId: string,
file: string | null,
language: string | null,
moves: LiveRunMove[],
) {
await axios.post(
"http://localhost:3000/api/run/move",
{
runId,
file,
moves,
language,
},
{
headers: {
+45 -10
View File
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
import { diff_match_patch, Diff } from "diff-match-patch";
import { LiveRunMove } from "./types";
import { addRunMoves } from "./client";
import path from "path";
export function startMonitoring(runId: string) {
const dmp = new diff_match_patch();
@@ -16,12 +17,11 @@ export function startMonitoring(runId: string) {
const IDLE_MS = 350;
const editor = vscode.window.activeTextEditor;
if (editor) {
lastText = editor.document.getText();
if (vscode.window.activeTextEditor) {
lastText = vscode.window.activeTextEditor.document.getText();
}
async function flush() {
async function flush(file: string | null, language: string | null) {
if (!moves.length || isSending) return;
isSending = true;
@@ -29,12 +29,19 @@ export function startMonitoring(runId: string) {
moves = [];
try {
await addRunMoves(runId, batch);
await addRunMoves(runId, file, language, batch);
} finally {
isSending = false;
}
}
function getLatency(): number {
const now = Date.now();
const latency = capLatency(now - lastEventTime, IDLE_MS);
lastEventTime = now;
return latency;
}
const readInterval = setInterval(() => {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
@@ -42,9 +49,7 @@ export function startMonitoring(runId: string) {
const newText = editor.document.getText();
if (newText === lastText) return;
const now = Date.now();
const latency = capLatency(now - lastEventTime, IDLE_MS);
lastEventTime = now;
let latency = getLatency();
const diffs = dmp.diff_main(lastText, newText);
dmp.diff_cleanupEfficiency(diffs);
@@ -63,13 +68,43 @@ export function startMonitoring(runId: string) {
}
if (idleTimeout) clearTimeout(idleTimeout);
idleTimeout = setTimeout(flush, IDLE_MS);
idleTimeout = setTimeout(
() =>
flush(
path.basename(editor?.document.fileName || "") || null,
editor?.document.languageId || null,
),
IDLE_MS,
);
}, 500);
vscode.window.onDidChangeActiveTextEditor((editor) => {
let latency = getLatency();
if (idleTimeout) clearTimeout(idleTimeout);
moves = [];
moves.push({
moveId: moveId++,
latency,
cursor: editor?.document.offsetAt(editor.selection.active) || 0,
});
flush(
path.basename(editor?.document.fileName || "") || null,
editor?.document.languageId || null,
);
});
return () => {
clearInterval(readInterval);
if (idleTimeout) clearTimeout(idleTimeout);
flush();
flush(
path.basename(vscode.window.activeTextEditor?.document.fileName || "") ||
null,
vscode.window.activeTextEditor?.document.languageId || null,
);
};
}