pub struct Patch<'a, Ns, Tag, Leaf, Att, Val>where
Ns: PartialEq + Clone + Debug,
Tag: PartialEq + Debug,
Leaf: PartialEq + Clone + Debug,
Att: PartialEq + Clone + Debug,
Val: PartialEq + Clone + Debug,{
pub tag: Option<&'a Tag>,
pub patch_path: TreePath,
pub patch_type: PatchType<'a, Ns, Tag, Leaf, Att, Val>,
}
Expand description
A Patch encodes an operation that modifies a real DOM element or native UI element
To update the real DOM that a user sees you’ll want to first diff your old virtual dom and new virtual dom.
This diff operation will generate Vec<Patch>
with zero or more patches that, when
applied to your real DOM, will make your real DOM look like your new virtual dom.
Each of the Patch contains TreePath
which contains an array of indexes for each node
that we need to traverse to get the target element.
Consider the following html:
<body>
<main>
<input type="text"/>
<img src="pic.jpg"/>
</main>
<footer>
<a>Link</a>
<nav/>
</footer>
</body>
The corresponding DOM tree would be
.─.
( 0 ) <body>
`-'
/ \
/ \
/ \
▼ ▼
<main> .─. .─. <footer>
( 0 ) ( 1 )
`-' `-'
/ \ | \ '.
/ \ | \ '.
▼ ▼ | \ '.
.─. .─. ▼ ▼ ▼
( 0 ) ( 1 ) .─. .─. .─.
`─' `─' ( 0 ) ( 1 ) ( 2 )
<input> <img> `─' `─' `─'
<a> <Text> <nav>
To traverse to the <nav>
element we follow the TreePath([0,1,2]).
0 - is the root element which is always zero.
1 - is the footer
element since it is the 2nd element of the body.
2 - is the nav
element since it is the 3rd node in the footer
element.
Fields§
§tag: Option<&'a Tag>
the tag of the node at patch_path
patch_path: TreePath
the path to traverse to get to the target element
patch_type: PatchType<'a, Ns, Tag, Leaf, Att, Val>
the type of patch we are going to apply
Implementations§
source§impl<'a, Ns, Tag, Leaf, Att, Val> Patch<'a, Ns, Tag, Leaf, Att, Val>where
Ns: PartialEq + Clone + Debug,
Tag: PartialEq + Debug,
Leaf: PartialEq + Clone + Debug,
Att: PartialEq + Clone + Debug,
Val: PartialEq + Clone + Debug,
impl<'a, Ns, Tag, Leaf, Att, Val> Patch<'a, Ns, Tag, Leaf, Att, Val>where Ns: PartialEq + Clone + Debug, Tag: PartialEq + Debug, Leaf: PartialEq + Clone + Debug, Att: PartialEq + Clone + Debug, Val: PartialEq + Clone + Debug,
sourcepub fn path(&self) -> &TreePath
pub fn path(&self) -> &TreePath
return the path to traverse for this patch to get to the target Node
sourcepub fn insert_before_node(
tag: Option<&'a Tag>,
patch_path: TreePath,
nodes: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>>
) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
pub fn insert_before_node( tag: Option<&'a Tag>, patch_path: TreePath, nodes: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
create an InsertBeforeNode patch
sourcepub fn insert_after_node(
tag: Option<&'a Tag>,
patch_path: TreePath,
nodes: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>>
) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
pub fn insert_after_node( tag: Option<&'a Tag>, patch_path: TreePath, nodes: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
create an InsertAfterNode patch
sourcepub fn append_children(
tag: &'a Tag,
patch_path: TreePath,
children: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>>
) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
pub fn append_children( tag: &'a Tag, patch_path: TreePath, children: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
create a patch where we add children to the target node
sourcepub fn remove_node(
tag: Option<&'a Tag>,
patch_path: TreePath
) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
pub fn remove_node( tag: Option<&'a Tag>, patch_path: TreePath ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
create a patch where the target element that can be traverse using the patch path will be remove
sourcepub fn replace_node(
tag: Option<&'a Tag>,
patch_path: TreePath,
replacement: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>>
) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
pub fn replace_node( tag: Option<&'a Tag>, patch_path: TreePath, replacement: Vec<&'a Node<Ns, Tag, Leaf, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>
create a patch where a node is replaced by the replacement
node.
The target node to be replace is traverse using the patch_path