Skip to content

Instantly share code, notes, and snippets.

@lightsprint09
Last active April 25, 2020 17:27
Show Gist options
  • Save lightsprint09/328d43a03bf33853ff6d9bcb42f2c365 to your computer and use it in GitHub Desktop.
Save lightsprint09/328d43a03bf33853ff6d9bcb42f2c365 to your computer and use it in GitHub Desktop.
Automerge Swift API
struct MyModel {
let created: Date
var mutableData: [String]
var text: Text
}
let content = MyModel(created: Date(), mutableData: [], text: Text("hi"))
let document = Automerge<MyModel>(content: content)
// accessing data
document.created // Use dynamic member lookup
document.content.created // or just expose the whole content
document.change(message: "change", { doc in
doc.[\.mutableData, "\.mutableData"].set(["hi", "hello"])
//Static keyPath helpf to get compile support
// dynamic keypath is needed, because there is no KeyPath Reflection/Serialization at the moment
doc.[\.mutableData, "\.mutableData"].append("good morning")
// Append is compiled checked, because the Key Paths knows it is an array
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment