pub struct Patch<'a, MSG> {
pub tag: Option<&'a &'static str>,
pub patch_path: TreePath,
pub patch_type: PatchType<'a, MSG>,
}
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 &'static str>
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, MSG>
the type of patch we are going to apply
Implementations§
source§impl<'a, MSG> Patch<'a, MSG>
impl<'a, MSG> Patch<'a, MSG>
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 node_paths(&self) -> &[TreePath]
pub fn node_paths(&self) -> &[TreePath]
return the node paths involve such as those in moving nodes
sourcepub fn insert_before_node(
tag: Option<&'a &'static str>,
patch_path: TreePath,
nodes: impl IntoIterator<Item = &'a Node<MSG>>
) -> Patch<'a, MSG>
pub fn insert_before_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes: impl IntoIterator<Item = &'a Node<MSG>> ) -> Patch<'a, MSG>
create an InsertBeforeNode patch
sourcepub fn insert_after_node(
tag: Option<&'a &'static str>,
patch_path: TreePath,
nodes: Vec<&'a Node<MSG>>
) -> Patch<'a, MSG>
pub fn insert_after_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes: Vec<&'a Node<MSG>> ) -> Patch<'a, MSG>
create an InsertAfterNode patch
sourcepub fn append_children(
tag: Option<&'a &'static str>,
patch_path: TreePath,
children: Vec<&'a Node<MSG>>
) -> Patch<'a, MSG>
pub fn append_children( tag: Option<&'a &'static str>, patch_path: TreePath, children: Vec<&'a Node<MSG>> ) -> Patch<'a, MSG>
create a patch where we add children to the target node
sourcepub fn remove_node(
tag: Option<&'a &'static str>,
patch_path: TreePath
) -> Patch<'a, MSG>
pub fn remove_node( tag: Option<&'a &'static str>, patch_path: TreePath ) -> Patch<'a, MSG>
create a patch where the target element that can be traverse using the patch path will be remove
sourcepub fn clear_children(
tag: Option<&'a &'static str>,
patch_path: TreePath
) -> Patch<'a, MSG>
pub fn clear_children( tag: Option<&'a &'static str>, patch_path: TreePath ) -> Patch<'a, MSG>
create a patch where the target element has to clear its children nodes
sourcepub fn move_before_node(
tag: Option<&'a &'static str>,
patch_path: TreePath,
nodes_path: impl IntoIterator<Item = TreePath>
) -> Patch<'a, MSG>
pub fn move_before_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes_path: impl IntoIterator<Item = TreePath> ) -> Patch<'a, MSG>
remove the nodes pointed at the nodes_path
and insert them before the target element
pointed at patch_path
sourcepub fn move_after_node(
tag: Option<&'a &'static str>,
patch_path: TreePath,
nodes_path: impl IntoIterator<Item = TreePath>
) -> Patch<'a, MSG>
pub fn move_after_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes_path: impl IntoIterator<Item = TreePath> ) -> Patch<'a, MSG>
remove the nodes pointed at the nodes_path
and insert them after the target element
pointed at patch_path
sourcepub fn replace_node(
tag: Option<&'a &'static str>,
patch_path: TreePath,
replacement: impl IntoIterator<Item = &'a Node<MSG>>
) -> Patch<'a, MSG>
pub fn replace_node( tag: Option<&'a &'static str>, patch_path: TreePath, replacement: impl IntoIterator<Item = &'a Node<MSG>> ) -> Patch<'a, MSG>
create a patch where a node is replaced by the replacement
node.
The target node to be replace is traverse using the patch_path
sourcepub fn add_attributes(
tag: &'a &'static str,
patch_path: TreePath,
attrs: impl IntoIterator<Item = &'a Attribute<MSG>>
) -> Patch<'a, MSG>
pub fn add_attributes( tag: &'a &'static str, patch_path: TreePath, attrs: impl IntoIterator<Item = &'a Attribute<MSG>> ) -> Patch<'a, MSG>
create a patch where a new attribute is added to the target element
Trait Implementations§
source§impl<'a, MSG> PartialEq for Patch<'a, MSG>
impl<'a, MSG> PartialEq for Patch<'a, MSG>
impl<'a, MSG> Eq for Patch<'a, MSG>
Auto Trait Implementations§
impl<'a, MSG> Freeze for Patch<'a, MSG>
impl<'a, MSG> !RefUnwindSafe for Patch<'a, MSG>
impl<'a, MSG> !Send for Patch<'a, MSG>
impl<'a, MSG> !Sync for Patch<'a, MSG>
impl<'a, MSG> Unpin for Patch<'a, MSG>
impl<'a, MSG> !UnwindSafe for Patch<'a, MSG>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.