@stephen/automerge-patcher
v0.8.0-alpha.6-patchobject
Published
Utility functions to apply and invert patches generated by Automerge document changes.
Downloads
5
Readme
Automerge Patcher
Utility functions to apply and invert patches generated by Automerge document changes.
Installation
npm install @onsetsoftware/automerge-patcher
Usage
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