Skip to content

Commit f2504f4

Browse files
bakapeteymour-aldridgecectonranile
authored
yew/vlist: optimize diffing and patching (#1555)
* yew/vlist: optimize diffing and patching * yew/vlist: fix key difference point detection * yew-macro: update stderr * vlist: store on the list, if all the children are keyed * yew/vlist: simplify iterators * yew/vlist: more short-circuiting during diff * yew/vlist: clone Rcs instead of referencing * yew/vlist: remove redundant branching * yew/vlist: remove newline * yew/vlist: remove false comment * yew: partial vector deconstruction * Apply suggestions from code review Co-authored-by: Teymour Aldridge <[email protected]> Co-authored-by: Cecile Tonglet <[email protected]> * yew: use links in doc comments * Update packages/yew/src/virtual_dom/mod.rs Co-authored-by: Muhammad Hamza <[email protected]> * yew: update insert_node() calls Co-authored-by: Teymour Aldridge <[email protected]> Co-authored-by: Cecile Tonglet <[email protected]> Co-authored-by: Muhammad Hamza <[email protected]>
1 parent 4a14d0f commit f2504f4

File tree

11 files changed

+356
-178
lines changed

11 files changed

+356
-178
lines changed

packages/yew-macro/src/html_tree/html_element.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ impl ToTokens for HtmlElement {
221221
quote! { ::std::vec![#(#listeners_it),*].into_iter().flatten().collect() }
222222
};
223223

224+
// TODO: if none of the children have possibly None expressions or literals as keys, we can
225+
// compute `VList.fully_keyed` at compile time.
224226
let child_list = quote! {
225-
::yew::virtual_dom::VList{
226-
key: ::std::option::Option::None,
227-
children: #children,
228-
}
227+
::yew::virtual_dom::VList::with_children(
228+
#children,
229+
::std::option::Option::None,
230+
)
229231
};
230232

231233
tokens.extend(match &name {

packages/yew-macro/src/html_tree/html_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl ToTokens for HtmlList {
7777

7878
tokens.extend(quote_spanned! {spanned.span()=>
7979
::yew::virtual_dom::VNode::VList(
80-
::yew::virtual_dom::VList::new_with_children(#children, #key)
80+
::yew::virtual_dom::VList::with_children(#children, #key)
8181
)
8282
});
8383
}

packages/yew/src/html/component/scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<COMP: Component> Scope<COMP> {
172172
) {
173173
let placeholder = {
174174
let placeholder: Node = document().create_text_node("").into();
175-
insert_node(&placeholder, &parent, next_sibling.get());
175+
insert_node(&placeholder, &parent, next_sibling.get().as_ref());
176176
node_ref.set(Some(placeholder.clone()));
177177
VNode::VRef(placeholder)
178178
};

packages/yew/src/html/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub type Html = VNode;
6666
/// ```
6767
/// ## Relevant examples
6868
/// - [Node Refs](https://github.com/yewstack/yew/tree/master/examples/node_refs)
69-
#[derive(Debug, Default, Clone)]
69+
#[derive(Default, Clone)]
7070
pub struct NodeRef(Rc<RefCell<NodeRefInner>>);
7171

7272
impl PartialEq for NodeRef {
@@ -75,6 +75,16 @@ impl PartialEq for NodeRef {
7575
}
7676
}
7777

78+
impl std::fmt::Debug for NodeRef {
79+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80+
write!(
81+
f,
82+
"NodeRef {{ references: {:?} }}",
83+
self.get().map(|n| crate::utils::print_node(&n))
84+
)
85+
}
86+
}
87+
7888
#[derive(PartialEq, Debug, Default, Clone)]
7989
struct NodeRefInner {
8090
node: Option<Node>,

packages/yew/src/utils/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,13 @@ impl<IN, OUT> IntoIterator for NodeSeq<IN, OUT> {
9494
self.0.into_iter()
9595
}
9696
}
97+
98+
/// Print the [web_sys::Node]'s contents as a string for debugging purposes
99+
pub fn print_node(n: &web_sys::Node) -> String {
100+
use wasm_bindgen::JsCast;
101+
102+
match n.dyn_ref::<web_sys::Element>() {
103+
Some(el) => el.outer_html(),
104+
None => n.text_content().unwrap_or_default(),
105+
}
106+
}

packages/yew/src/virtual_dom/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,10 @@ pub(crate) trait VDiff {
422422
) -> NodeRef;
423423
}
424424

425-
pub(crate) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<Node>) {
425+
pub(crate) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<&Node>) {
426426
match next_sibling {
427427
Some(next_sibling) => parent
428-
.insert_before(&node, Some(&next_sibling))
428+
.insert_before(&node, Some(next_sibling))
429429
.expect("failed to insert tag before next sibling"),
430430
None => parent.append_child(node).expect("failed to append child"),
431431
};

packages/yew/src/virtual_dom/vcomp.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl VComp {
9999
}
100100
}
101101

102-
#[allow(unused)]
103102
pub(crate) fn root_vnode(&self) -> Option<impl Deref<Target = VNode> + '_> {
104103
self.scope.as_ref().and_then(|scope| scope.root_vnode())
105104
}
@@ -202,7 +201,7 @@ impl PartialEq for VComp {
202201

203202
impl fmt::Debug for VComp {
204203
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205-
f.write_str("VComp")
204+
write!(f, "VComp {{ root: {:?} }}", self.root_vnode().as_deref())
206205
}
207206
}
208207

0 commit comments

Comments
 (0)