agnostic/vdom/patch

Types

A Change represents a single modification to a DOM node (including re-ordering or removing its children).

Note: when constructing a Change you should always use the provided constructors to ensure that the kind field is set correctly, never construct a variant directly.

pub type Change(message) {
  ReplaceText(kind: Int, content: String)
  ReplaceRawContent(kind: Int, content: vnode.RawContent)
  ReplaceRawNode(kind: Int, with: vnode.Element(message))
  Update(
    kind: Int,
    added: List(vattr.Attribute(message)),
    removed: List(vattr.Attribute(message)),
  )
  Move(kind: Int, key: String, before: Int)
  Replace(kind: Int, index: Int, with: vnode.Element(message))
  Remove(kind: Int, index: Int)
  Insert(
    kind: Int,
    children: List(vnode.Element(message)),
    before: Int,
  )
}

Constructors

  • ReplaceText(kind: Int, content: String)
  • ReplaceRawContent(kind: Int, content: vnode.RawContent)
  • ReplaceRawNode(kind: Int, with: vnode.Element(message))
  • Update(
      kind: Int,
      added: List(vattr.Attribute(message)),
      removed: List(vattr.Attribute(message)),
    )
  • Move(kind: Int, key: String, before: Int)

    Move a keyed child so that it is before the child at the given index.

  • Replace(kind: Int, index: Int, with: vnode.Element(message))

    Replace a node at the given index with a new vnode.

  • Remove(kind: Int, index: Int)

    Remove a child at the given index.

  • Insert(
      kind: Int,
      children: List(vnode.Element(message)),
      before: Int,
    )

    Insert one or multiple children before the child with the given index.

A patch represents a set of precise changes needed to update the real DOM to match a diffed VDOM. It is made up of four parts:

  • An index which is the index of the node in the real DOM relative to its parent’s childNodes list.

  • A path which represents any additional traversal to get to the relevant node. This happens when we have a change deep in the tree and no changes to any of the nodes above it. By compressing the path into a single list we can cut down on the payload size of diffs sent over the wire in server components.

  • A removed count for the number of child nodes to remove after the list of changes has been applied.

  • A list of changes that modify the DOM node or the order of its children in some way.

  • A list of children patches that represents a traversal into the node’s children for further patching.

pub type Patch(message) {
  Patch(
    index: Int,
    path: List(Int),
    removed: Int,
    changes: List(Change(message)),
    children: List(Patch(message)),
  )
}

Constructors

  • Patch(
      index: Int,
      path: List(Int),
      removed: Int,
      changes: List(Change(message)),
      children: List(Patch(message)),
    )

Values

pub fn add_parent(
  child: Patch(message),
  index: Int,
) -> Patch(message)
pub fn insert(
  children children: List(vnode.Element(message)),
  before before: Int,
) -> Change(message)
pub const insert_kind: Int
pub fn is_empty(patch: Patch(message)) -> Bool
pub fn move(
  key key: String,
  before before: Int,
) -> Change(message)
pub const move_kind: Int
pub fn new(
  index index: Int,
  removed removed: Int,
  changes changes: List(Change(message)),
  children children: List(Patch(message)),
) -> Patch(message)
pub fn remove(index index: Int) -> Change(message)
pub const remove_kind: Int
pub fn replace(
  index index: Int,
  with with: vnode.Element(message),
) -> Change(message)
pub const replace_kind: Int
pub fn replace_raw_content(
  content content: vnode.RawContent,
) -> Change(message)
pub const replace_raw_content_kind: Int
pub fn replace_raw_node(
  with with: vnode.Element(message),
) -> Change(message)
pub const replace_raw_node_kind: Int
pub fn replace_text(content content: String) -> Change(message)
pub const replace_text_kind: Int
pub fn to_json(
  patch: Patch(message),
  memos: mutable_map.MutableMap(
    fn() -> vnode.Element(message),
    vnode.Element(message),
  ),
  serialize_raw_content: fn(vnode.RawContent) -> String,
) -> json.Json
pub fn update(
  added added: List(vattr.Attribute(message)),
  removed removed: List(vattr.Attribute(message)),
) -> Change(message)
pub const update_kind: Int
Search Document