From 4376d780c09a772c944d0622f2db2fa5861bcd3b Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 17 Feb 2026 03:27:02 +0100 Subject: [PATCH 1/3] Small changes --- src/client.ts | 7 +++++++ src/extension.ts | 2 ++ src/move.ts | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 8591d9a..88533a0 100644 --- a/src/client.ts +++ b/src/client.ts @@ -176,6 +176,13 @@ async function send( Public API ────────────────────────── */ +export async function closeClient() { + if (!socket) return; + socket.close(); + socket = null; + connecting = null; +} + export async function addRun( useLocalhost: boolean, problem: string, diff --git a/src/extension.ts b/src/extension.ts index 385b938..4a1e5c2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,6 +4,7 @@ import { startServer, stopServer } from "./server"; import os from "os"; import { exec } from "child_process"; import net from "net"; +import { closeClient } from "./client"; var globalStatusBarItem: vscode.StatusBarItem | undefined; @@ -105,6 +106,7 @@ export function activate(context: vscode.ExtensionContext) { export function deactivate() { stopServer(globalStatusBarItem as any); + closeClient(); } export function canConnect( diff --git a/src/move.ts b/src/move.ts index 8b9a1a9..12db055 100644 --- a/src/move.ts +++ b/src/move.ts @@ -129,7 +129,7 @@ export function startMonitoring(useLocalhost: boolean, runId: string) { moveId: moveId++, changes: { from: 0, - to: serverText.length, + to: serverText?.length || 0, insert: editorText, }, }, From 1304c5c7f00e72bd40a440c8f0775d26200d8dd9 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 17 Feb 2026 03:34:49 +0100 Subject: [PATCH 2/3] ping pong --- src/client.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/client.ts b/src/client.ts index 88533a0..657f947 100644 --- a/src/client.ts +++ b/src/client.ts @@ -133,6 +133,10 @@ async function ensureSocket(useLocalhost: boolean): Promise { currentOrigin = null; connecting = null; }; + + ws.on("ping", () => { + ws.pong(); + }); }); return connecting; From bd76c0b86c0ceed1ff5425975d46ed18092c9f12 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 17 Feb 2026 16:02:29 +0100 Subject: [PATCH 3/3] Improved a few things --- src/client.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/client.ts b/src/client.ts index 657f947..517381b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -69,10 +69,7 @@ async function ensureSocket(useLocalhost: boolean): Promise { return connecting; } - if (socket && currentOrigin !== origin) { - socket.close(); - socket = null; - } + if (socket && currentOrigin !== origin) closeClient(); connecting = new Promise((resolve, reject) => { const ws = new WebSocket(origin); @@ -88,7 +85,7 @@ async function ensureSocket(useLocalhost: boolean): Promise { currentOrigin = origin; connecting = null; - await sendUnsafe(useLocalhost, { + await sendUnsafe({ type: "auth", cookies: authCookies, }); @@ -146,10 +143,7 @@ async function ensureSocket(useLocalhost: boolean): Promise { RPC helpers ────────────────────────── */ -async function sendUnsafe( - useLocalhost: boolean, - payload: Record, -): Promise { +async function sendUnsafe(payload: Record): Promise { if (!socket || socket.readyState !== WebSocket.OPEN) { throw new Error("WebSocket is not open"); } @@ -164,7 +158,11 @@ async function sendUnsafe( pending.set(requestId, { resolve, reject, timeout }); - socket?.send(JSON.stringify({ ...payload, requestId })); + if (!socket || socket.readyState !== WebSocket.OPEN) { + throw new Error("WebSocket closed while sending"); + } + + socket.send(JSON.stringify({ ...payload, requestId })); }); } @@ -173,7 +171,7 @@ async function send( payload: Record, ): Promise { await ensureSocket(useLocalhost); - return sendUnsafe(useLocalhost, payload); + return sendUnsafe(payload); } /* ──────────────────────────