Compare commits
10
Commits
4520eaf861
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63bf006350 | ||
|
|
bd76c0b86c | ||
|
|
1304c5c7f0 | ||
|
|
4376d780c0 | ||
|
|
9dd60a56de | ||
|
|
10bf1ae31d | ||
|
|
62c49404cb | ||
|
|
fde6a4ccb9 | ||
|
|
38d53f0a6d | ||
|
|
f3b780c637 |
+146
-26
@@ -2,79 +2,189 @@ import { WebSocket } from "ws";
|
|||||||
import { LiveRunMove } from "./types";
|
import { LiveRunMove } from "./types";
|
||||||
import { randomUUID } from "crypto";
|
import { randomUUID } from "crypto";
|
||||||
|
|
||||||
|
/* ──────────────────────────
|
||||||
|
State
|
||||||
|
────────────────────────── */
|
||||||
|
|
||||||
let socket: WebSocket | null = null;
|
let socket: WebSocket | null = null;
|
||||||
let cookies: string | undefined;
|
let connecting: Promise<WebSocket> | null = null;
|
||||||
|
let currentOrigin: string | null = null;
|
||||||
|
|
||||||
|
let cookies: Record<string, string> | undefined;
|
||||||
|
|
||||||
type PendingResolver = {
|
type PendingResolver = {
|
||||||
resolve: (value: any) => void;
|
resolve: (value: any) => void;
|
||||||
reject: (err: any) => void;
|
reject: (err: any) => void;
|
||||||
|
timeout: NodeJS.Timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
const pending = new Map<string, PendingResolver>();
|
const pending = new Map<string, PendingResolver>();
|
||||||
|
|
||||||
export function getCookies(): string | undefined {
|
/* ──────────────────────────
|
||||||
|
Cookies
|
||||||
|
────────────────────────── */
|
||||||
|
|
||||||
|
export function getCookies(): Record<string, string> | undefined {
|
||||||
return cookies;
|
return cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setCookies(c: string) {
|
export function setCookies(c: Record<string, string>) {
|
||||||
cookies = c;
|
cookies = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ──────────────────────────
|
||||||
|
Utils
|
||||||
|
────────────────────────── */
|
||||||
|
|
||||||
function getWsOrigin(useLocalhost: boolean) {
|
function getWsOrigin(useLocalhost: boolean) {
|
||||||
return useLocalhost
|
return useLocalhost
|
||||||
? "ws://localhost:3000/api/run"
|
? "ws://localhost:3000/api/run"
|
||||||
: "wss://dev-run.selimaj.dev/api/run";
|
: "wss://dev-run.selimaj.dev/api/run";
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureSocket(useLocalhost: boolean): WebSocket {
|
function rejectAllPending(err: Error) {
|
||||||
if (socket && socket.readyState === WebSocket.OPEN) {
|
for (const { reject, timeout } of pending.values()) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
pending.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ──────────────────────────
|
||||||
|
Socket management
|
||||||
|
────────────────────────── */
|
||||||
|
|
||||||
|
async function ensureSocket(useLocalhost: boolean): Promise<WebSocket> {
|
||||||
|
const origin = getWsOrigin(useLocalhost);
|
||||||
|
|
||||||
|
if (
|
||||||
|
socket &&
|
||||||
|
socket.readyState === WebSocket.OPEN &&
|
||||||
|
currentOrigin === origin
|
||||||
|
) {
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket = new WebSocket(getWsOrigin(useLocalhost));
|
if (connecting) {
|
||||||
|
return connecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (socket && currentOrigin !== origin) closeClient();
|
||||||
|
|
||||||
|
connecting = new Promise<WebSocket>((resolve, reject) => {
|
||||||
|
const ws = new WebSocket(origin);
|
||||||
|
|
||||||
|
ws.onopen = async () => {
|
||||||
|
try {
|
||||||
|
const authCookies = getCookies();
|
||||||
|
if (!authCookies) {
|
||||||
|
throw new Error("Cookies not set");
|
||||||
|
}
|
||||||
|
|
||||||
|
socket = ws;
|
||||||
|
currentOrigin = origin;
|
||||||
|
connecting = null;
|
||||||
|
|
||||||
|
await sendUnsafe({
|
||||||
|
type: "auth",
|
||||||
|
cookies: authCookies,
|
||||||
|
});
|
||||||
|
|
||||||
|
resolve(ws);
|
||||||
|
} catch (err) {
|
||||||
|
ws.close();
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = (e) => {
|
||||||
|
let msg: any;
|
||||||
|
try {
|
||||||
|
msg = JSON.parse(e.data.toString());
|
||||||
|
} catch {
|
||||||
|
console.warn("Invalid WS message:", e.data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
socket.onmessage = (e) => {
|
|
||||||
const msg = JSON.parse(e.data.toString());
|
|
||||||
const { requestId, ok, data, error } = msg;
|
const { requestId, ok, data, error } = msg;
|
||||||
|
|
||||||
if (!requestId) return;
|
if (!requestId) return;
|
||||||
|
|
||||||
const pendingReq = pending.get(requestId);
|
const pendingReq = pending.get(requestId);
|
||||||
if (!pendingReq) return;
|
if (!pendingReq) return;
|
||||||
|
|
||||||
pending.delete(requestId);
|
pending.delete(requestId);
|
||||||
|
clearTimeout(pendingReq.timeout);
|
||||||
|
|
||||||
ok ? pendingReq.resolve(data) : pendingReq.reject(error);
|
ok ? pendingReq.resolve(data) : pendingReq.reject(error);
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onclose = () => {
|
ws.onerror = (err) => {
|
||||||
socket = null;
|
rejectAllPending(new Error("WebSocket error"));
|
||||||
|
connecting = null;
|
||||||
|
reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
return socket;
|
ws.onclose = () => {
|
||||||
|
rejectAllPending(new Error("WebSocket closed"));
|
||||||
|
socket = null;
|
||||||
|
currentOrigin = null;
|
||||||
|
connecting = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.on("ping", () => {
|
||||||
|
ws.pong();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return connecting;
|
||||||
}
|
}
|
||||||
|
|
||||||
function send<T>(
|
/* ──────────────────────────
|
||||||
useLocalhost: boolean,
|
RPC helpers
|
||||||
payload: Record<string, any>,
|
────────────────────────── */
|
||||||
): Promise<T> {
|
|
||||||
return new Promise((resolve, reject) => {
|
async function sendUnsafe<T>(payload: Record<string, any>): Promise<T> {
|
||||||
const ws = ensureSocket(useLocalhost);
|
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||||
|
throw new Error("WebSocket is not open");
|
||||||
|
}
|
||||||
|
|
||||||
const requestId = randomUUID();
|
const requestId = randomUUID();
|
||||||
|
|
||||||
pending.set(requestId, { resolve, reject });
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
pending.delete(requestId);
|
||||||
|
reject(new Error("WebSocket request timed out"));
|
||||||
|
}, 10_000);
|
||||||
|
|
||||||
ws.send(
|
pending.set(requestId, { resolve, reject, timeout });
|
||||||
JSON.stringify({
|
|
||||||
...payload,
|
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||||
requestId,
|
throw new Error("WebSocket closed while sending");
|
||||||
cookies, // optional: only needed if you authenticate via cookies manually
|
}
|
||||||
}),
|
|
||||||
);
|
socket.send(JSON.stringify({ ...payload, requestId }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function send<T>(
|
||||||
|
useLocalhost: boolean,
|
||||||
|
payload: Record<string, any>,
|
||||||
|
): Promise<T> {
|
||||||
|
await ensureSocket(useLocalhost);
|
||||||
|
return sendUnsafe(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ──────────────────────────
|
||||||
|
Public API
|
||||||
|
────────────────────────── */
|
||||||
|
|
||||||
|
export async function closeClient() {
|
||||||
|
if (!socket) return;
|
||||||
|
socket.close();
|
||||||
|
socket = null;
|
||||||
|
connecting = null;
|
||||||
|
}
|
||||||
|
|
||||||
export async function addRun(
|
export async function addRun(
|
||||||
useLocalhost: boolean,
|
useLocalhost: boolean,
|
||||||
problem: string,
|
problem: string,
|
||||||
@@ -111,3 +221,13 @@ export async function addRunMoves(
|
|||||||
moves,
|
moves,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getText(
|
||||||
|
useLocalhost: boolean,
|
||||||
|
runId: string,
|
||||||
|
): Promise<string> {
|
||||||
|
return await send(useLocalhost, {
|
||||||
|
type: "getText",
|
||||||
|
runId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { startServer, stopServer } from "./server";
|
|||||||
import os from "os";
|
import os from "os";
|
||||||
import { exec } from "child_process";
|
import { exec } from "child_process";
|
||||||
import net from "net";
|
import net from "net";
|
||||||
|
import { closeClient } from "./client";
|
||||||
|
|
||||||
var globalStatusBarItem: vscode.StatusBarItem | undefined;
|
var globalStatusBarItem: vscode.StatusBarItem | undefined;
|
||||||
|
|
||||||
@@ -105,6 +106,7 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
|
|
||||||
export function deactivate() {
|
export function deactivate() {
|
||||||
stopServer(globalStatusBarItem as any);
|
stopServer(globalStatusBarItem as any);
|
||||||
|
closeClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function canConnect(
|
export function canConnect(
|
||||||
|
|||||||
+69
-4
@@ -1,7 +1,7 @@
|
|||||||
import * as vscode from "vscode";
|
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, getText } from "./client";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
export function startMonitoring(useLocalhost: boolean, runId: string) {
|
export function startMonitoring(useLocalhost: boolean, runId: string) {
|
||||||
@@ -11,6 +11,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
|
|||||||
let moves: LiveRunMove[] = [];
|
let moves: LiveRunMove[] = [];
|
||||||
|
|
||||||
let lastText = "";
|
let lastText = "";
|
||||||
|
let lastCursorOffset: number | undefined;
|
||||||
let lastEventTime = Date.now();
|
let lastEventTime = Date.now();
|
||||||
let idleTimeout: NodeJS.Timeout | undefined;
|
let idleTimeout: NodeJS.Timeout | undefined;
|
||||||
let isSending = false;
|
let isSending = false;
|
||||||
@@ -18,7 +19,9 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
|
|||||||
const IDLE_MS = 350;
|
const IDLE_MS = 350;
|
||||||
|
|
||||||
if (vscode.window.activeTextEditor) {
|
if (vscode.window.activeTextEditor) {
|
||||||
lastText = vscode.window.activeTextEditor.document.getText();
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
lastText = editor.document.getText();
|
||||||
|
lastCursorOffset = editor.document.offsetAt(editor.selection.active);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function flush(file: string | null, language: string | null) {
|
async function flush(file: string | null, language: string | null) {
|
||||||
@@ -42,13 +45,13 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
|
|||||||
return latency;
|
return latency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Existing text change interval ---
|
||||||
const readInterval = setInterval(() => {
|
const readInterval = setInterval(() => {
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|
||||||
const newText = editor.document.getText();
|
const newText = editor.document.getText();
|
||||||
if (newText === lastText) return;
|
if (newText !== lastText) {
|
||||||
|
|
||||||
let latency = getLatency();
|
let latency = getLatency();
|
||||||
|
|
||||||
const diffs = dmp.diff_main(lastText, newText);
|
const diffs = dmp.diff_main(lastText, newText);
|
||||||
@@ -76,8 +79,68 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
|
|||||||
),
|
),
|
||||||
IDLE_MS,
|
IDLE_MS,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
||||||
|
// --- New cursor tracking interval ---
|
||||||
|
const cursorInterval = setInterval(() => {
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
const cursorOffset = editor.document.offsetAt(editor.selection.active);
|
||||||
|
|
||||||
|
if (cursorOffset !== lastCursorOffset) {
|
||||||
|
const latency = getLatency();
|
||||||
|
|
||||||
|
moves.push({
|
||||||
|
moveId: moveId++,
|
||||||
|
latency,
|
||||||
|
cursor: cursorOffset,
|
||||||
|
});
|
||||||
|
|
||||||
|
lastCursorOffset = cursorOffset;
|
||||||
|
|
||||||
|
if (idleTimeout) clearTimeout(idleTimeout);
|
||||||
|
idleTimeout = setTimeout(
|
||||||
|
() =>
|
||||||
|
flush(
|
||||||
|
path.basename(editor.document.fileName || "") || null,
|
||||||
|
editor.document.languageId || null,
|
||||||
|
),
|
||||||
|
IDLE_MS,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, 1000); // every second
|
||||||
|
|
||||||
|
// Sync text every 5 seconds to prevent desync
|
||||||
|
const textStabilityInterval = setInterval(async () => {
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (!editor) return;
|
||||||
|
|
||||||
|
const serverText = await getText(useLocalhost, runId);
|
||||||
|
|
||||||
|
const editorText = editor.document.getText();
|
||||||
|
if (editorText !== serverText) {
|
||||||
|
clearTimeout(idleTimeout);
|
||||||
|
moves = [
|
||||||
|
{
|
||||||
|
latency: 0,
|
||||||
|
cursor: editor.document.offsetAt(editor.selection.active),
|
||||||
|
moveId: moveId++,
|
||||||
|
changes: {
|
||||||
|
from: 0,
|
||||||
|
to: serverText?.length || 0,
|
||||||
|
insert: editorText,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
flush(
|
||||||
|
path.basename(editor?.document.fileName || "") || null,
|
||||||
|
editor?.document.languageId || null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, 5000); // every 5 seconds
|
||||||
|
|
||||||
vscode.window.onDidChangeActiveTextEditor((editor) => {
|
vscode.window.onDidChangeActiveTextEditor((editor) => {
|
||||||
let latency = getLatency();
|
let latency = getLatency();
|
||||||
|
|
||||||
@@ -98,7 +161,9 @@ export function startMonitoring(useLocalhost: boolean, runId: string) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
clearInterval(textStabilityInterval);
|
||||||
clearInterval(readInterval);
|
clearInterval(readInterval);
|
||||||
|
clearInterval(cursorInterval);
|
||||||
if (idleTimeout) clearTimeout(idleTimeout);
|
if (idleTimeout) clearTimeout(idleTimeout);
|
||||||
flush(
|
flush(
|
||||||
path.basename(vscode.window.activeTextEditor?.document.fileName || "") ||
|
path.basename(vscode.window.activeTextEditor?.document.fileName || "") ||
|
||||||
|
|||||||
Reference in New Issue
Block a user