public onMessage(change: MessageEvent<any>) {
let [newDoc] = Automerge.applyChanges(globalBoardState, change.data)
applyLatestChange(newDoc)
this.view.render();
}
public send() {
let lastChange = Automerge.getLastLocalChange(globalBoardState)
Automerge.save(globalBoardState)
this.channel.postMessage(lastChange);
}
I am trying to use Automerge to update the state of an online Whiteboard for multiple users. In the send() function I use Automerge.getLastLocalChange() of the current state of the board of one users to get the latest applied change and then forward it to the other users by using the postMessage() function.
In the onMessage() function I try to use the Automerge.applyChanges() function to apply the latest change from the other user to their current globalBoardState. This is then stored in the variable newDoc where I use my function applyLatestChange(new Doc),
export function applyLatestChange(newDoc: Automerge.Doc<BoardState>) {
globalBoardState = newDoc;
}
which just overrides the current globalBoardState with the newDoc.
This however does not work, as the function Automerge.applyChanges expects an uint8array, which I would only get by using the function Automerge.getChanges(). This however would require me to store the state of the whiteboard before applying a change, then using the function on the old and the current state, which then would only give me the change, just as the getLastLocalChange() function, except it has the right data type.
https://automerge.org/docs/cookbook/real-time/
In the documentation for Automerge it states "If you only need the last change you just made, you can call Automerge.getLastLocalChange(newDoc), which is faster.", which would be ideal for my case as I am trying to update the state after every change.
I have tried mapping it into an Array of Uint8arrays, which is the expected input for the applyChanges() function, but I wasn't able to do this properly.
In a previous version I also just sent the entire boardstate, but I'm trying to optimize it by just sending the latest change.