I’m trying to write a simple binding to the CRDT library automerge
and I’m having some trouble figuring out how to type the change function nicely. I’ve looked around for some other ReScript libraries that use Proxy objects like this, and haven’t found anything, but here is what I have so far:
module Automerge = {
module Document = {
type t<'a> = {..} as 'a
}
@module("automerge")
external make: 'a => Document.t<'a> = "init"
@module("automerge")
external change: (Document.t<'a>, @uncurry ('a => unit)) => Document.t<'a> =
"change"
}
type person = {@set "name": string, @set "phone": string}
let raw = {"name": "Scott", "phone": "000000000"}
let doc1 = raw->Automerge.make // Automerge.Document.t<{ "name": string; "phone": string }>
let doc2 = doc1->Automerge.change(
%raw(`
function (draft) { // Trying to get this to be a mutable object
draft["name"] = "Scott Trinh"
draft["phone"] = "111111111"
}`),
)
let check = doc2["name"] == "Scott Trinh" // true
—I’m also very interested in general feedback on what I have so far in my external binding here.