automerge

package module
v0.0.0-...-a89ad3c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 5 Imported by: 0

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 automerge.min.js@1.0.1-preview.7, 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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRuntime *goja.Runtime

Functions

func GetHistory

func GetHistory(doc *Object) (h []map[string]map[string]interface{})

func Save

func Save(doc *Object) []byte

Types

type Object

type Object struct {
	*goja.Object
	// contains filtered or unexported fields
}

func Change

func Change(doc *Object, message string, changeFn func(*Object)) *Object

func Init

func Init() *Object

func Load

func Load(data []byte) *Object

func Merge

func Merge(doc1 *Object, doc2 *Object) *Object

func ToObject

func ToObject(v goja.Value) *Object

func (*Object) Call

func (obj *Object) Call(name string, args ...interface{}) *Object

func (*Object) Export

func (obj *Object) Export() interface{}

func (*Object) Get

func (obj *Object) Get(name string) *Object

func (*Object) NewArray

func (obj *Object) NewArray(items ...interface{}) *goja.Object

func (*Object) NewObject

func (obj *Object) NewObject() *goja.Object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL