automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.77k stars 467 forks source link

get change message(s) from Automerge.getChanges() #534

Open michaelpalumbo opened 12 months ago

michaelpalumbo commented 12 months ago

TLDR: Is it possible to get filter changes returned by getChanges() according to the message in the changeOption? How is this done?

I have change messages assigned to each of the possible edits in my app, i.e.


// message 'update position'
this.document = Automerge.change(this.document, 'update position', doc => {
  doc[posID].position = payload[1]
}) 

// and message 'add cable':
this.document = Automerge.change(this.document, 'add cable', doc => {
  for(let i=0; i<doc[srcID].outputs.length; i++){
    // find the output in the doc matching the cable's src
    if(doc[srcID].outputs[i].name == srcJack){
      if(!doc[srcID].outputs[i].connections[destID]){
        doc[srcID].outputs[i].connections[destID] = {}
      }
      doc[srcID].outputs[i].connections[destID][destJack] = cableType
    }
  }     
})

These two document changes are handled quite differently in the app: the first one results in changes to a visual scene, while the second results in changes to both visuals and what is hear in the audio output.

I recently implemented the Automerge Sync protocol, which is working great. Each time a syncMessage has been received and processed, I want to run Automerge.getChanges() and get the change message per each change so that I can trigger the additional steps related to each edit type.

ept commented 11 months ago

Hi @michaelpalumbo, you can pass a change (the byte array) to Automerge.decodeChange() to get back a JSON representation that includes the message. Does that do what you need?