refactor headless sync

This commit is contained in:
Nystik
2026-03-30 21:05:47 +02:00
parent ecad257587
commit 300e251734
14 changed files with 619 additions and 223 deletions

View File

@@ -6,6 +6,9 @@ const watcher = require("./watcher");
function setupWebSocket(server) {
const wss = new WebSocketServer({ server, path: "/ws" });
// Plugin-registered message handlers: type -> handler(msg, ws)
wss.messageHandlers = new Map();
wss.on("connection", (ws, req) => {
const params = new url.URL(req.url, "http://localhost").searchParams;
const vaultId = params.get("vault");
@@ -30,6 +33,18 @@ function setupWebSocket(server) {
watcher.addListener(vaultId, listener);
// Dispatch incoming messages to registered handlers
ws.on("message", (data) => {
try {
const msg = JSON.parse(data);
const handler = wss.messageHandlers.get(msg.type);
if (handler) {
handler(msg, ws);
}
} catch {}
});
ws.on("close", () => {
console.log(`[ws] Client disconnected from vault: ${vaultId}`);
watcher.removeListener(vaultId, listener);