Skip to content

Commit

Permalink
feat: map interoperability for yjs
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Sep 19, 2023
1 parent aeef826 commit cb791b3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 26 deletions.
2 changes: 0 additions & 2 deletions y-octo-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
"@napi-rs/cli": "^2.16.2",
"@types/node": "^18.17.5",
"@types/prompts": "^2.4.4",
"@types/uuid": "^9.0.2",
"c8": "^8.0.1",
"prompts": "^2.4.2",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"uuid": "^9.0.0",
"yjs": "^13.6.8"
},
"engines": {
Expand Down
1 change: 0 additions & 1 deletion y-octo-node/tests/doc.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ test("doc test", { concurrency: false }, async (t) => {
let text = doc.getOrCreateText("text");

equal(array.length, 4);
console.log(array.toJson(), array.get(0), array.get(1), array.get(2), array.get(3) ,array.get(4));
equal(array.get(0), true);
equal(array.get(1), false);
equal(array.get(2), 1);
Expand Down
94 changes: 92 additions & 2 deletions y-octo-node/tests/map.spec.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import assert, { equal, deepEqual } from "node:assert";
import { test } from "node:test";

import { Doc, YMap } from "../index";
import * as Y from "yjs";
import { Doc, YArray, YMap, YText } from "../index";

test("map test", { concurrency: false }, async (t) => {
let client_id: number;
Expand Down Expand Up @@ -40,7 +41,7 @@ test("map test", { concurrency: false }, async (t) => {
equal(map.get("d"), "hello world");
});

await t.test("sub map should can edit", () => {
await t.test("map should can be nested", () => {
let map = doc.getOrCreateMap("map");
let sub = doc.createMap();
map.setMap("sub", sub);
Expand All @@ -59,4 +60,93 @@ test("map test", { concurrency: false }, async (t) => {
equal(sub2.get("d"), "hello world");
equal(sub2.length, 4);
});

await t.test("y-octo to yjs compatibility test with nested type", () => {
let map = doc.getOrCreateMap("map");
let sub_array = doc.createArray();
let sub_map = doc.createMap();
let sub_text = doc.createText();

map.setArray("array", sub_array);
map.setMap("map", sub_map);
map.setText("text", sub_text);

sub_array.insert(0, true);
sub_array.insert(1, false);
sub_array.insert(2, 1);
sub_array.insert(3, "hello world");
sub_map.set("a", true);
sub_map.set("b", false);
sub_map.set("c", 1);
sub_map.set("d", "hello world");
sub_text.insert(0, "a");
sub_text.insert(1, "b");
sub_text.insert(2, "c");

let doc2 = new Y.Doc();
Y.applyUpdate(doc2, doc.encodeUpdateV1());

let map2 = doc2.getMap<any>("map");
let sub_array2 = map2.get("array") as Y.Array<any>;
let sub_map2 = map2.get("map") as Y.Map<any>;
let sub_text2 = map2.get("text") as Y.Text;

assert(sub_array2);
equal(sub_array2.length, 4);
equal(sub_array2.get(0), true);
equal(sub_array2.get(1), false);
equal(sub_array2.get(2), 1);
equal(sub_array2.get(3), "hello world");
assert(sub_map2);
equal(sub_map2.get("a"), true);
equal(sub_map2.get("b"), false);
equal(sub_map2.get("c"), 1);
equal(sub_map2.get("d"), "hello world");
assert(sub_text2);
equal(sub_text2.toString(), "abc");
});

await t.test("yjs to y-octo compatibility test with nested type", () => {
let doc2 = new Y.Doc();
let map2 = doc2.getMap<any>("map");
let sub_array2 = new Y.Array<any>();
let sub_map2 = new Y.Map<any>();
let sub_text2 = new Y.Text();
map2.set("array", sub_array2);
map2.set("map", sub_map2);
map2.set("text", sub_text2);

sub_array2.insert(0, [true]);
sub_array2.insert(1, [false]);
sub_array2.insert(2, [1]);
sub_array2.insert(3, ["hello world"]);
sub_map2.set("a", true);
sub_map2.set("b", false);
sub_map2.set("c", 1);
sub_map2.set("d", "hello world");
sub_text2.insert(0, "a");
sub_text2.insert(1, "b");
sub_text2.insert(2, "c");

doc.applyUpdate(Buffer.from(Y.encodeStateAsUpdate(doc2)));

let map = doc.getOrCreateMap("map");
let sub_array = map.get<YArray>("array");
let sub_map = map.get<YMap>("map");
let sub_text = map.get<YText>("text");

assert(sub_array);
equal(sub_array.length, 4);
equal(sub_array.get(0), true);
equal(sub_array.get(1), false);
equal(sub_array.get(2), 1);
equal(sub_array.get(3), "hello world");
assert(sub_map);
equal(sub_map.get("a"), true);
equal(sub_map.get("b"), false);
equal(sub_map.get("c"), 1);
equal(sub_map.get("d"), "hello world");
assert(sub_text);
equal(sub_text.toString(), "abc");
});
});
24 changes: 3 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ __metadata:
linkType: hard

"@types/node@npm:*":
version: 20.5.1
resolution: "@types/node@npm:20.5.1"
checksum: 3dbe611cd67afa987102c8558ee70f848949c5dcfee5f60abc073e55c0d7b048e391bf06bb1e0dc052cb7210ca97136ac496cbaf6e89123c989de6bd125fde82
version: 20.6.2
resolution: "@types/node@npm:20.6.2"
checksum: 96fe5303872640a173f3fd43e289a451776ed5b8f0090094447c6790b43f23fb607eea8268af0829cef4d132e5afa0bfa4cd871aa7412e9042a414a698e9e971
languageName: node
linkType: hard

Expand All @@ -146,13 +146,6 @@ __metadata:
languageName: node
linkType: hard

"@types/uuid@npm:^9.0.2":
version: 9.0.2
resolution: "@types/uuid@npm:9.0.2"
checksum: 1754bcf3444e1e3aeadd6e774fc328eb53bc956665e2e8fb6ec127aa8e1f43d9a224c3d22a9a6233dca8dd81a12dc7fed4d84b8876dd5ec82d40f574f7ff8b68
languageName: node
linkType: hard

"@y-octo/cli@workspace:.":
version: 0.0.0-use.local
resolution: "@y-octo/cli@workspace:."
Expand All @@ -172,12 +165,10 @@ __metadata:
"@napi-rs/cli": ^2.16.2
"@types/node": ^18.17.5
"@types/prompts": ^2.4.4
"@types/uuid": ^9.0.2
c8: ^8.0.1
prompts: ^2.4.2
ts-node: ^10.9.1
typescript: ^5.1.6
uuid: ^9.0.0
yjs: ^13.6.8
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -1514,15 +1505,6 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:^9.0.0":
version: 9.0.0
resolution: "uuid@npm:9.0.0"
bin:
uuid: dist/bin/uuid
checksum: 8dd2c83c43ddc7e1c71e36b60aea40030a6505139af6bee0f382ebcd1a56f6cd3028f7f06ffb07f8cf6ced320b76aea275284b224b002b289f89fe89c389b028
languageName: node
linkType: hard

"v8-compile-cache-lib@npm:^3.0.1":
version: 3.0.1
resolution: "v8-compile-cache-lib@npm:3.0.1"
Expand Down

0 comments on commit cb791b3

Please sign in to comment.