Tab change
This commit is contained in:
+8
-1
@@ -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(
|
await axios.post(
|
||||||
"http://localhost:3000/api/run/move",
|
"http://localhost:3000/api/run/move",
|
||||||
{
|
{
|
||||||
runId,
|
runId,
|
||||||
|
file,
|
||||||
moves,
|
moves,
|
||||||
|
language,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
+45
-10
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
|
|||||||
import { diff_match_patch, Diff } from "diff-match-patch";
|
import { diff_match_patch, Diff } from "diff-match-patch";
|
||||||
import { LiveRunMove } from "./types";
|
import { LiveRunMove } from "./types";
|
||||||
import { addRunMoves } from "./client";
|
import { addRunMoves } from "./client";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export function startMonitoring(runId: string) {
|
export function startMonitoring(runId: string) {
|
||||||
const dmp = new diff_match_patch();
|
const dmp = new diff_match_patch();
|
||||||
@@ -16,12 +17,11 @@ export function startMonitoring(runId: string) {
|
|||||||
|
|
||||||
const IDLE_MS = 350;
|
const IDLE_MS = 350;
|
||||||
|
|
||||||
const editor = vscode.window.activeTextEditor;
|
if (vscode.window.activeTextEditor) {
|
||||||
if (editor) {
|
lastText = vscode.window.activeTextEditor.document.getText();
|
||||||
lastText = editor.document.getText();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function flush() {
|
async function flush(file: string | null, language: string | null) {
|
||||||
if (!moves.length || isSending) return;
|
if (!moves.length || isSending) return;
|
||||||
|
|
||||||
isSending = true;
|
isSending = true;
|
||||||
@@ -29,12 +29,19 @@ export function startMonitoring(runId: string) {
|
|||||||
moves = [];
|
moves = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await addRunMoves(runId, batch);
|
await addRunMoves(runId, file, language, batch);
|
||||||
} finally {
|
} finally {
|
||||||
isSending = false;
|
isSending = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getLatency(): number {
|
||||||
|
const now = Date.now();
|
||||||
|
const latency = capLatency(now - lastEventTime, IDLE_MS);
|
||||||
|
lastEventTime = now;
|
||||||
|
return latency;
|
||||||
|
}
|
||||||
|
|
||||||
const readInterval = setInterval(() => {
|
const readInterval = setInterval(() => {
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
@@ -42,9 +49,7 @@ export function startMonitoring(runId: string) {
|
|||||||
const newText = editor.document.getText();
|
const newText = editor.document.getText();
|
||||||
if (newText === lastText) return;
|
if (newText === lastText) return;
|
||||||
|
|
||||||
const now = Date.now();
|
let latency = getLatency();
|
||||||
const latency = capLatency(now - lastEventTime, IDLE_MS);
|
|
||||||
lastEventTime = now;
|
|
||||||
|
|
||||||
const diffs = dmp.diff_main(lastText, newText);
|
const diffs = dmp.diff_main(lastText, newText);
|
||||||
dmp.diff_cleanupEfficiency(diffs);
|
dmp.diff_cleanupEfficiency(diffs);
|
||||||
@@ -63,13 +68,43 @@ export function startMonitoring(runId: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (idleTimeout) clearTimeout(idleTimeout);
|
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);
|
}, 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 () => {
|
return () => {
|
||||||
clearInterval(readInterval);
|
clearInterval(readInterval);
|
||||||
if (idleTimeout) clearTimeout(idleTimeout);
|
if (idleTimeout) clearTimeout(idleTimeout);
|
||||||
flush();
|
flush(
|
||||||
|
path.basename(vscode.window.activeTextEditor?.document.fileName || "") ||
|
||||||
|
null,
|
||||||
|
vscode.window.activeTextEditor?.document.languageId || null,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user