Fix suppression flag timing with counter instead of boolean
This commit is contained in:
parent
73ee70c52a
commit
7b81777d9d
1 changed files with 5 additions and 5 deletions
|
|
@ -9,7 +9,7 @@ const DAEMON_URL = process.env.COLLABD_URL || "ws://localhost:4040/ws";
|
||||||
let ws: WebSocket | null = null;
|
let ws: WebSocket | null = null;
|
||||||
let doc: Y.Doc | null = null;
|
let doc: Y.Doc | null = null;
|
||||||
let text: Y.Text | null = null;
|
let text: Y.Text | null = null;
|
||||||
let suppressLocal = false;
|
let suppressLocal = 0;
|
||||||
|
|
||||||
function send(msg: object) {
|
function send(msg: object) {
|
||||||
console.log(JSON.stringify(msg));
|
console.log(JSON.stringify(msg));
|
||||||
|
|
@ -24,7 +24,7 @@ function connect(roomName: string) {
|
||||||
|
|
||||||
// when remote changes come in, notify vim
|
// when remote changes come in, notify vim
|
||||||
text.observe(() => {
|
text.observe(() => {
|
||||||
if (!suppressLocal) {
|
if (suppressLocal === 0) {
|
||||||
send({ type: "content", text: text?.toString() || "" });
|
send({ type: "content", text: text?.toString() || "" });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -43,12 +43,12 @@ function connect(roomName: string) {
|
||||||
case "update": {
|
case "update": {
|
||||||
if (!doc) break;
|
if (!doc) break;
|
||||||
try {
|
try {
|
||||||
suppressLocal = true;
|
suppressLocal++;
|
||||||
Y.applyUpdate(doc, new Uint8Array(msg.data));
|
Y.applyUpdate(doc, new Uint8Array(msg.data));
|
||||||
suppressLocal = false;
|
suppressLocal--;
|
||||||
send({ type: "content", text: text?.toString() || "" });
|
send({ type: "content", text: text?.toString() || "" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
suppressLocal = false;
|
suppressLocal--;
|
||||||
console.error("failed to apply yjs update:", err);
|
console.error("failed to apply yjs update:", err);
|
||||||
send({ type: "error", message: "failed to apply remote update" });
|
send({ type: "error", message: "failed to apply remote update" });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue