Last active
July 14, 2023 15:48
-
-
Save stephen/b94acb049f50418a4780cede85f79dd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Automerge = require("@automerge/automerge"); | |
const fs = require("fs"); | |
const bytes = fs.readFileSync(process.argv[2]); | |
let doc = Automerge.load(bytes); | |
let msg; | |
let peer = Automerge.init(); | |
let docSync = Automerge.initSyncState(); | |
let peerSync = Automerge.initSyncState(); | |
do { | |
if (msg) { | |
[doc, docSync, msg] = Automerge.receiveSyncMessage(doc, docSync, msg); | |
} | |
[docSync, msg] = Automerge.generateSyncMessage(doc, docSync); | |
if (!msg) { | |
break; | |
} | |
[peer, peerSync] = Automerge.receiveSyncMessage(peer, peerSync, msg); | |
[peerSync, msg] = Automerge.generateSyncMessage(peer, peerSync); | |
console.log("syncing"); | |
} while (msg); | |
Automerge.load(Automerge.save(peer)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
"github.com/automerge/automerge-go" | |
"github.com/davecgh/go-spew/spew" | |
) | |
func main() { | |
data, err := os.ReadFile(os.Args[1]) | |
if err != nil { | |
panic(err) | |
} | |
doc, err := automerge.Load(data) | |
if err != nil { | |
panic(err) | |
} | |
log.Println("doc", spew.Sdump(doc.RootMap())) | |
changes, err := doc.Changes() | |
if err != nil { | |
panic(err) | |
} | |
log.Println("changes", len(changes)) | |
docSync := automerge.NewSyncState(doc) | |
peerDoc := automerge.New() | |
peerSync := automerge.NewSyncState(peerDoc) | |
var msg []byte | |
for { | |
if msg != nil { | |
if err := docSync.ReceiveMessage(msg); err != nil { | |
panic(err) | |
} | |
} | |
msg, _ = docSync.GenerateMessage() | |
if err != nil { | |
panic(err) | |
} | |
if msg == nil { | |
break | |
} | |
if err := peerSync.ReceiveMessage(msg); err != nil { | |
panic(err) | |
} | |
msg, _ = peerSync.GenerateMessage() | |
} | |
log.Println("peer", spew.Sdump(peerDoc.RootMap())) | |
reloaded, err := automerge.Load(peerDoc.Save()) | |
if err != nil { | |
panic(err) | |
} | |
log.Println("reloaded peer", spew.Sdump(reloaded.RootMap())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment