[don't merge this] A very rough sketch of Automerge support in the Nova playground. #15

Open
pvh wants to merge 31 commits from pvh/myte:automerge into main
First-time contributor
No description provided.
@ -10,0 +14,4 @@
isValidAutomergeUrl,
initializeWasm,
updateText,
} = await import("https://esm.sh/@automerge/vanillajs/slim?standalone");
Author
First-time contributor

Here we import the Automerge-specific dependencies: the Repo which holds documents, browser-friendly networking and storage adapters (many others exist but these are pretty good starting points), and a couple of helper functions.

Here we import the Automerge-specific dependencies: the Repo which holds documents, browser-friendly networking and storage adapters (many others exist but these are pretty good starting points), and a couple of helper functions.
@ -10,0 +18,4 @@
await initializeWasm(
fetch("https://esm.sh/@automerge/automerge@2.2.9/dist/automerge.wasm")
);
Author
First-time contributor

The core of Automerge is written in Rust and made available via webassembly. Most bundlers will use the non slim version of the library and handle loading the wasm object automatically. In a vanilla environment we need this annoying line until the browser vendors decide how to support depending on WASM files will work. (Any year now, I hope.)

The core of Automerge is written in Rust and made available via webassembly. Most bundlers will use the non `slim` version of the library and handle loading the wasm object automatically. In a vanilla environment we need this annoying line until the browser vendors decide how to support depending on WASM files will work. (Any year now, I hope.)
@ -10,0 +23,4 @@
const repo = new Repo({
storage: new IndexedDBStorageAdapter(),
network: [new WebSocketClientAdapter("wss://sync3.automerge.org")],
});
Author
First-time contributor

This is the main bit of setup: we create a repository that will hold documents and configure it to write locally and synchronize with a sync server. You could also add other network adapters like BroadcastChannel to handle local in-browser synchronization, or you could sync to other sync servers.

This is the main bit of setup: we create a repository that will hold documents and configure it to write locally and synchronize with a sync server. You could also add other network adapters like BroadcastChannel to handle local in-browser synchronization, or you could sync to other sync servers.
@ -10,0 +42,4 @@
console.log("DocHandle", handle);
const docUrl = (document.location.hash = handle.url);
Author
First-time contributor

This code (from line 30) either loads a document from the hash in the URL or else creates a document and puts it there. It's a little bit lazy in its implementation because it doesn't watch the hash: if you change the hash you need to manually reload.

This code (from line 30) either loads a document from the hash in the URL or else creates a document and puts it there. It's a little bit lazy in its implementation because it doesn't watch the hash: if you change the hash you need to manually reload.
@ -37,2 +70,2 @@
codeElem.value = shareProgram;
}
codeElem.value = handle.doc().code;
handle.on("change", ({ doc }) => (codeElem.value = doc.code));
Author
First-time contributor

And this is the meat of the patch! First, if the document changes, set the text area's value to a particular field in the document.

And this is the meat of the patch! First, if the document changes, set the text area's value to a particular field in the document.
@ -42,1 +73,3 @@
}
codeElem.addEventListener("input", function (event) {
handle.change((doc) => updateText(doc, ["code"], event.target.value));
});
Author
First-time contributor

And second, when the text area changes, update the text in the CRDT. Note the use of updateText -- this means that your edits will be diffed against the current value you have for the CRDT and applied as a patch rather than replacing the whole program on each keystroke.

And second, when the text area changes, update the text in the CRDT. Note the use of updateText -- this means that your edits will be diffed against the current value you have for the CRDT and applied as a patch rather than replacing the whole program on each keystroke.
This pull request has changes conflicting with the target branch.
  • .forgejo/workflows/deploy.yml
  • examples/browser_playground/playground_js.nv
  • examples/browser_playground/project.nv
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u automerge:pvh-automerge
git switch pvh-automerge

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch main
git merge --no-ff pvh-automerge
git switch pvh-automerge
git rebase main
git switch main
git merge --ff-only pvh-automerge
git switch pvh-automerge
git rebase main
git switch main
git merge --no-ff pvh-automerge
git switch main
git merge --squash pvh-automerge
git switch main
git merge --ff-only pvh-automerge
git switch main
git merge pvh-automerge
git push origin main
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: yumaikas/myte#15
No description provided.