Skip to content

Commit

Permalink
Redo #2957.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Apr 24, 2022
1 parent 45a4faf commit 0565411
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 40 deletions.
25 changes: 18 additions & 7 deletions packages/yew/src/dom_bundle/bcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ use crate::NodeRef;
pub(super) struct BComp {
type_id: TypeId,
scope: Box<dyn Scoped>,
// A internal NodeRef passed around to track this components position. This
// is "stable", i.e. does not change when reconciled.
internal_ref: NodeRef,
// The user-passed NodeRef from VComp. Might change every time we reconcile.
// Gets linked to the internal ref
node_ref: NodeRef,
key: Option<Key>,
}
Expand Down Expand Up @@ -62,20 +67,23 @@ impl Reconcilable for VComp {
node_ref,
key,
} = self;
let internal_ref = NodeRef::default();
node_ref.link(internal_ref.clone());

let scope = mountable.mount(
root,
node_ref.clone(),
internal_ref.clone(),
parent_scope,
parent.to_owned(),
next_sibling,
);

(
node_ref.clone(),
internal_ref.clone(),
BComp {
type_id,
node_ref,
internal_ref,
key,
scope,
},
Expand Down Expand Up @@ -117,10 +125,10 @@ impl Reconcilable for VComp {
} = self;

bcomp.key = key;
let old_ref = std::mem::replace(&mut bcomp.node_ref, node_ref.clone());
let old_ref = std::mem::replace(&mut bcomp.node_ref, node_ref);
bcomp.node_ref.reuse(old_ref);
mountable.reuse(node_ref.clone(), bcomp.scope.borrow(), next_sibling);
node_ref
mountable.reuse(bcomp.scope.borrow(), next_sibling);
bcomp.internal_ref.clone()
}
}

Expand All @@ -143,21 +151,24 @@ mod feat_hydration {
node_ref,
key,
} = self;
let internal_ref = NodeRef::default();
node_ref.link(internal_ref.clone());

let scoped = mountable.hydrate(
root.clone(),
parent_scope,
parent.clone(),
fragment,
node_ref.clone(),
internal_ref.clone(),
);

(
node_ref.clone(),
internal_ref.clone(),
BComp {
type_id,
scope: scoped,
node_ref,
internal_ref,
key,
},
)
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/dom_bundle/blist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ mod feat_hydration {
let (child_node_ref, child) = child.hydrate(root, parent_scope, parent, fragment);

if index == 0 {
node_ref.reuse(child_node_ref);
node_ref.link(child_node_ref);
}

children.push(child);
Expand Down
5 changes: 4 additions & 1 deletion packages/yew/src/dom_bundle/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ pub(super) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<&N
match next_sibling {
Some(next_sibling) => parent
.insert_before(node, Some(next_sibling))
.expect("failed to insert tag before next sibling"),
.unwrap_or_else(|err| {
gloo::console::error!("failed to insert node", err, parent, next_sibling, node);
panic!("failed to insert tag before next sibling")
}),
None => parent.append_child(node).expect("failed to append child"),
};
}
Expand Down
16 changes: 0 additions & 16 deletions packages/yew/src/html/component/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,13 @@ impl<COMP: BaseComponent> Runnable for CreateRunner<COMP> {
pub(crate) struct PropsUpdateRunner {
pub props: Rc<dyn Any>,
pub state: Shared<Option<ComponentState>>,
pub node_ref: NodeRef,
pub next_sibling: NodeRef,
}

#[cfg(feature = "csr")]
impl Runnable for PropsUpdateRunner {
fn run(self: Box<Self>) {
let Self {
node_ref: next_node_ref,
next_sibling,
props,
state: shared_state,
Expand All @@ -323,16 +321,9 @@ impl Runnable for PropsUpdateRunner {
let schedule_render = match state.render_state {
#[cfg(feature = "csr")]
ComponentRenderState::Render {
ref mut node_ref,
next_sibling: ref mut current_next_sibling,
..
} => {
// When components are updated, a new node ref could have been passed in
if *node_ref != next_node_ref {
next_node_ref.set(node_ref.get());
node_ref.set(None);
*node_ref = next_node_ref;
}
// When components are updated, their siblings were likely also updated
*current_next_sibling = next_sibling;
// Only trigger changed if props were changed
Expand All @@ -341,16 +332,9 @@ impl Runnable for PropsUpdateRunner {

#[cfg(feature = "hydration")]
ComponentRenderState::Hydration {
ref mut node_ref,
next_sibling: ref mut current_next_sibling,
..
} => {
// When components are updated, a new node ref could have been passed in
if *node_ref != next_node_ref {
next_node_ref.set(node_ref.get());
node_ref.set(None);
*node_ref = next_node_ref;
}
// When components are updated, their siblings were likely also updated
*current_next_sibling = next_sibling;
// Only trigger changed if props were changed
Expand Down
11 changes: 2 additions & 9 deletions packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,10 @@ mod feat_csr {
fn schedule_props_update(
state: Shared<Option<ComponentState>>,
props: Rc<dyn Any>,
node_ref: NodeRef,
next_sibling: NodeRef,
) {
scheduler::push_component_props_update(Box::new(PropsUpdateRunner {
state,
node_ref,
next_sibling,
props,
}));
Expand Down Expand Up @@ -471,16 +469,11 @@ mod feat_csr {
scheduler::start();
}

pub(crate) fn reuse(
&self,
props: Rc<COMP::Properties>,
node_ref: NodeRef,
next_sibling: NodeRef,
) {
pub(crate) fn reuse(&self, props: Rc<COMP::Properties>, next_sibling: NodeRef) {
#[cfg(debug_assertions)]
super::super::log_event(self.id, "reuse");

schedule_props_update(self.state.clone(), props, node_ref, next_sibling)
schedule_props_update(self.state.clone(), props, next_sibling)
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/yew/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ mod feat_csr {
}

let mut this = self.0.borrow_mut();
let existing = node_ref.0.borrow();
this.node = existing.node.clone();
this.link = existing.link.clone();
let mut existing = node_ref.0.borrow_mut();
this.node = existing.node.take();
this.link = existing.link.take();
}

/// Link a downstream `NodeRef`
Expand Down
6 changes: 3 additions & 3 deletions packages/yew/src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) trait Mountable {
) -> Box<dyn Scoped>;

#[cfg(feature = "csr")]
fn reuse(self: Box<Self>, node_ref: NodeRef, scope: &dyn Scoped, next_sibling: NodeRef);
fn reuse(self: Box<Self>, scope: &dyn Scoped, next_sibling: NodeRef);

#[cfg(feature = "ssr")]
fn render_to_string<'a>(
Expand Down Expand Up @@ -119,9 +119,9 @@ impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
}

#[cfg(feature = "csr")]
fn reuse(self: Box<Self>, node_ref: NodeRef, scope: &dyn Scoped, next_sibling: NodeRef) {
fn reuse(self: Box<Self>, scope: &dyn Scoped, next_sibling: NodeRef) {
let scope: Scope<COMP> = scope.to_any().downcast::<COMP>();
scope.reuse(self.props, node_ref, next_sibling);
scope.reuse(self.props, next_sibling);
}

#[cfg(feature = "ssr")]
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
edition = "2021"

format_code_in_doc_comments = true
wrap_comments = true
comment_width = 100 # same as default max_width
Expand Down

0 comments on commit 0565411

Please sign in to comment.