Skip to content

Utility functions to apply and invert patches generated by Automerge document changes.

License

Notifications You must be signed in to change notification settings

tsukkiren/automerge-patcher

 
 

Repository files navigation

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

About

Utility functions to apply and invert patches generated by Automerge document changes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%