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.76k stars 467 forks source link

Automerge.diff : how to port the old app to automerge v1.0 if Automerge.diff is removed ? #434

Closed raphael10-collab closed 2 years ago

raphael10-collab commented 2 years ago

Hi! I need to know how to port in old app the Automerge.diff if it is remove from automerge v1.0

Example:

const opCreate = ({ obj, type }: Automerge.diff, [map, ops]: any) => {
  map[obj] = createByType(type)

  return [map, ops]
}

How to substitute Automerge.diff ?

ept commented 2 years ago

Hi @raphael10-collab, one way is to register a patchCallback function which gets called every time a document is updated:

let doc = Automerge.init({patchCallback(patch) {
  console.log(patch)
}})
doc = Automerge.change(doc, doc => doc.x = 'y')

The format of the patch is somewhat different to the Automerge 0.x diff format, but it contains essentially the same information. The type definitions document the structure of a patch.

Does that help?

raphael10-collab commented 2 years ago

So.... as far as I understand, at the moment, to get Automerge.diff I can use something like this in type.d.ts :

let doc = Automerge.init({patchCallback(patch) {
  return [doc, patch.diff]
}})

----> doc[1] is the "old" Automerge.diff ?

ept commented 2 years ago

Returning a value from the patchCallback will have no effect since its return value is ignored. As I said, the diff field in the patch has a somewhat different structure from the old diff format, but it should hopefully be self-explanatory if you try it with a few examples, or look at the tests, which contain lots of examples of patches.

With the old Automerge.diff, you would pass in two document versions and get back the differences between them. The patchCallback is a bit different, because it gets called as soon as a change is applied. If you want to specifically compare two given document versions, you can do that by cloning the older version, applying the changes between the older and the newer version to the older version, and record the patch resulting from applying those changes.

raphael10-collab commented 2 years ago

Thank you Martin. I close the issue for now