Patches

Modifying a model does not only result in a new snapshot, but also in a stream of JSON-patches describing which modifications were made. Patches have the following signature:

export interface IJsonPatch {
    op: "replace" | "add" | "remove"
    path: string
    value?: any
}
  • Patches are constructed according to JSON-Patch, RFC 6902
  • Patches are emitted immediately when a mutation is made, and don't respect transaction boundaries (like snapshots)
  • Patch listeners can be used to achieve deep observing
  • The path attribute of a patch contains the path of the event, relative to the place where the event listener is attached.
  • A single mutation can result in multiple patches, for example, when splicing an array
  • Patches can be reverse applied, which enables many powerful patterns like undo / redo

Useful Methods

  • onPatch(model, listener): attaches a patch listener to the provided model, which will be invoked whenever the model or any of its descendants is mutated.
  • applyPatch(model, patch): applies a patch (or array of patches) to the provided model.
  • revertPatch(model, patch): reverse applies a patch (or array of patches) to the provided model. This replays the inverse of a set of patches to a model, which can be used to bring it back to its original state.

results matching ""

    No results matching ""