Created
August 27, 2019 18:30
-
-
Save jeffpeterson/e6719ecbb5af14d87119e69300516f91 to your computer and use it in GitHub Desktop.
Automerge frontend/backend split example
This file contains hidden or 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
import * as Backend from 'automerge/backend' | |
let doc = Backend.init() | |
loadChangesFromDisk().then(onRemoteChanges) | |
function onLocalChange(change) { | |
const [newDoc, patch] = Backend.applyLocalChange(doc, change) | |
doc = newDoc | |
saveChangeToDisk(change) | |
sendToFrontend(patch) | |
} | |
function onRemoteChanges(changes) { | |
const [newDoc, patch] = Backend.applyChanges(doc, changes) | |
doc = newDoc | |
sendToFrontend(patch) | |
} |
This file contains hidden or 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
import * as Frontend from 'automerge/frontend' | |
const actorId = "abc" | |
// This can also be set later with `doc = Frontend.setActorId(doc)`, | |
// but must be done before `Frontend.change` is called. | |
let doc = Frontend.init(actorId) | |
function onPatch(patch) { | |
doc = Frontend.applyPatch(doc, patch) | |
} | |
function makeChange(fn) { | |
const [newDoc, change] = Frontend.change(doc, fn) | |
doc = newDoc | |
sendToBackend(change) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment