Skip to content

Patches

A Patch records which records changed between two documents, with the old and new value of each.

type Change<T> =
| { readonly before: T; readonly after: T | undefined }
| { readonly before: undefined; readonly after: T };
type Patch = {
readonly shapes: ReadonlyMap<ShapeId, Change<Shape>>;
readonly bindings: ReadonlyMap<BindingId, Change<Binding>>;
};

An undefined before is an added record, and an undefined after a removed one. A change that is absent on both sides cannot be written.

Functions

  • diff(before, after) reads only the ids in after.touched. Each id whose record is not the same object in both documents becomes a change. after must derive from before through the document helpers. Equal data in a new object counts as a change, which is why updateShapes keeps the old object for a no-op.
  • apply(doc, patch) writes each after value, and removes the record when after is undefined. Applying a patch twice gives the same document as applying it once.
  • invert(patch) swaps before and after. Applying the inverse puts back the exact old objects.
  • isEmptyPatch(patch) is true when no shape and no binding changed.
  • changedShapeIds(before, after) returns the ids of shapes that were added, replaced or removed. Like diff, it reads only touched ids.