diff --git a/adapters/vim/bridge.ts b/adapters/vim/bridge.ts index 80f8ce6..cdee6c9 100644 --- a/adapters/vim/bridge.ts +++ b/adapters/vim/bridge.ts @@ -42,10 +42,16 @@ function connect(roomName: string) { case "sync": case "update": { if (!doc) break; - suppressLocal = true; - Y.applyUpdate(doc, new Uint8Array(msg.data)); - suppressLocal = false; - send({ type: "content", text: text?.toString() || "" }); + try { + suppressLocal = true; + Y.applyUpdate(doc, new Uint8Array(msg.data)); + suppressLocal = false; + send({ type: "content", text: text?.toString() || "" }); + } catch (err) { + suppressLocal = false; + console.error("failed to apply yjs update:", err); + send({ type: "error", message: "failed to apply remote update" }); + } break; } case "peers": { diff --git a/src/session.ts b/src/session.ts index 01763c8..dbde65b 100644 --- a/src/session.ts +++ b/src/session.ts @@ -28,11 +28,21 @@ export class Session { } applyUpdate(update: Uint8Array, from: Client) { - Y.applyUpdate(this.doc, update); - // broadcast to others - for (const client of this.clients) { - if (client !== from) { - client.ws.send(encode({ type: "update", data: Array.from(update) })); + try { + Y.applyUpdate(this.doc, update); + // broadcast to others + for (const client of this.clients) { + if (client !== from) { + client.ws.send(encode({ type: "update", data: Array.from(update) })); + } + } + } catch (err) { + console.error(`failed to apply update in session ${this.name}:`, err); + // optionally notify the client that sent the bad update + try { + from.ws.send(encode({ type: "error", message: "invalid update" } as any)); + } catch { + // ignore send errors } } }