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 inafter.touched. Each id whose record is not the same object in both documents becomes a change.aftermust derive frombeforethrough the document helpers. Equal data in a new object counts as a change, which is whyupdateShapeskeeps the old object for a no-op.apply(doc, patch)writes eachaftervalue, and removes the record whenafterisundefined. Applying a patch twice gives the same document as applying it once.invert(patch)swapsbeforeandafter. Applying the inverse puts back the exact old objects.isEmptyPatch(patch)istruewhen no shape and no binding changed.changedShapeIds(before, after)returns the ids of shapes that were added, replaced or removed. Likediff, it reads only touched ids.