Utility functions to apply and invert patches generated by Automerge document changes.
npm install @onsetsoftware/automerge-patcher
import { patch, unpatch } from '@onsetsoftware/automerge-patcher';
import {from} from "@automerge/automerge";
const doc = from({foo: 'bar'});
let patch;
const doc2 = change(
doc,
{
patchCallback: (p, old) => {
// capture inverse of each patch for undo
patch = unpatch(old, p);
}
},
(doc) => {
doc.foo = 'baz';
}
);
console.log(patch); // {action: 'put', path: ['foo'], value: 'bar'}
console.log(doc2.foo); // baz
const doc3 = change(doc2, (doc) => {
// apply the patch to revert the change
applyPatch(doc, patch);
});
console.log(doc3.foo); // bar