Skip to content

Commit

Permalink
Fix call stack exceeded bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Oct 18, 2020
1 parent cbf6a13 commit c126de8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 8 additions & 0 deletions yew/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ impl NodeRef {
this.node = None;
this.link = Some(node_ref);
}

/// Reuse an existing `NodeRef`
pub(crate) fn reuse(&self, node_ref: Self) {
let mut this = self.0.borrow_mut();
let existing = node_ref.0.borrow();
this.node = existing.node.clone();
this.link = existing.link.clone();
}
}

/// Trait for building properties for a component
Expand Down
25 changes: 23 additions & 2 deletions yew/src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl VDiff for VComp {
if let VNode::VComp(ref mut vcomp) = &mut ancestor {
// If the ancestor is the same type, reuse it and update its properties
if self.type_id == vcomp.type_id && self.key == vcomp.key {
self.node_ref.link(vcomp.node_ref.clone());
self.node_ref.reuse(vcomp.node_ref.clone());
let scope = vcomp.scope.take().expect("VComp is not mounted");
mountable.reuse(scope.borrow(), next_sibling);
self.scope = Some(scope);
Expand Down Expand Up @@ -316,14 +316,35 @@ mod tests {
}

fn change(&mut self, _: Self::Properties) -> ShouldRender {
unimplemented!();
true
}

fn view(&self) -> Html {
html! { <div/> }
}
}

#[test]
fn update_loop() {
let document = crate::utils::document();
let parent_scope: AnyScope = crate::html::Scope::<Comp>::new(None).into();
let parent_element = document.create_element("div").unwrap();

let mut ancestor = html! { <Comp></Comp> };
ancestor.apply(&parent_scope, &parent_element, NodeRef::default(), None);

for _ in 0..10000 {
let mut node = html! { <Comp></Comp> };
node.apply(
&parent_scope,
&parent_element,
NodeRef::default(),
Some(ancestor),
);
ancestor = node;
}
}

#[test]
fn set_properties_to_component() {
html! {
Expand Down

0 comments on commit c126de8

Please sign in to comment.