Categorygithub.com/progrium/goja-automerge
repositorypackage
0.0.0-20220805003334-a89ad3c9af35
Repository: https://github.com/progrium/goja-automerge.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

goja-automerge

Automerge.js in Go via goja

This is mostly for prototyping (for what should be obvious reasons), but this package runs automerge.js in native Go (no CGO) via goja, wrapping the API so it feels more like a native Go library:

doc1 := automerge.Init()
doc1 = automerge.Change(doc1, "add cards", func(doc *automerge.Object) {
  doc.Set("cards", doc.NewArray())
  doc.Get("cards").Call("push", map[string]interface{}{
    "title": "Rewrite everything in Go",
    "done":  false,
  })
  doc.Get("cards").Call("push", map[string]interface{}{
    "title": "Rewrite everything in Zig",
    "done":  false,
  })
})

b := automerge.Save(doc1)
doc2 := automerge.Load(b)

doc1 = automerge.Change(doc1, "mark card as done", func(doc *automerge.Object) {
  doc.Get("cards").Get("0").Set("done", true)
})
doc2 = automerge.Change(doc2, "delete card", func(doc *automerge.Object) {
  doc.Get("cards").Delete("1")
})

finalDoc := automerge.Merge(doc1, doc2)
d, _ := finalDoc.MarshalJSON()
fmt.Println(string(d))
// Output: {"cards":[{"done":true,"title":"Rewrite everything in Go"}]}

A more production usable version of this would probably use automerge-rs via WASM (again, to avoid CGO), but somebody else can put that together. Please.

The automerge.es5.js file is embedded in the package to be run in goja. This file was produced by taking [email protected], adding a polyfill for TextEncoder/TextDecoder, and running through Babel to get an ES5 version that can run in current version of goja.

License

MIT