Skip to content

Commit

Permalink
refactor JS test clients
Browse files Browse the repository at this point in the history
  • Loading branch information
dlqqq committed Feb 2, 2024
1 parent f75c614 commit 11597d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
20 changes: 8 additions & 12 deletions tests/yjs_client_0.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ wsProvider.on("status", (event) => {
console.log(event.status);
});

var clock = -1;

ymap.observe((event) => {
event.changes.keys.forEach((change, key) => {
if (key === "clock") {
const clk = ymap.get("clock");
if (clk > clock) {
ymap.set("out", ymap.get("in") + 1);
clock = clk + 1;
ymap.set("clock", clock);
}
}
});
// only do something when another client updates `ymap.clock`
if (event.transaction.local || !event.changes.keys.has("clock")) {
return;
}

const clock = ymap.get("clock");
ymap.set("out", ymap.get("in") + 1);
ymap.set("clock", clock + 1);
});
34 changes: 15 additions & 19 deletions tests/yjs_client_1.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@ wsProvider.on("status", (event) => {
console.log(event.status);
});

var clock = -1;

ymap.observe((event) => {
event.changes.keys.forEach((change, key) => {
if (key === "clock") {
const clk = ymap.get("clock");
if (clk > clock) {
const cells = [
new Y.Map([
["source", new Y.Text("1 + 2")],
["metadata", { foo: "bar" }],
]),
];
ycells.push(cells);
ystate.set("state", { dirty: false });
clock = clk + 1;
ymap.set("clock", clock);
}
}
});
// only do something when another client updates `ymap.clock`
if (event.transaction.local || !event.changes.keys.has("clock")) {
return;
}

const clock = ymap.get("clock");
const cells = [
new Y.Map([
["source", new Y.Text("1 + 2")],
["metadata", { foo: "bar" }],
]),
];
ycells.push(cells);
ystate.set("state", { dirty: false });
ymap.set("clock", clock + 1);
});

0 comments on commit 11597d5

Please sign in to comment.