Skip to content

Commit

Permalink
Revert "Merge branch 'hydration-4' into fc-prepared-state"
Browse files Browse the repository at this point in the history
This reverts commit 427b087d4db6b2e497ad618273655bd18ba9bd01, reversing
changes made to 109fcfa.
  • Loading branch information
futursolo committed Apr 24, 2022
1 parent d8edd96 commit f1e4089
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 574 deletions.
2 changes: 1 addition & 1 deletion examples/js_callback/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
wasm-bindgen = "0.2"
yew = { path = "../../packages/yew", features = ["csr", "tokio"] }
yew = { path = "../../packages/yew", features = ["csr"] }
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
once_cell = "1"
29 changes: 19 additions & 10 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 All @@ -39,10 +44,8 @@ impl ReconcileTarget for BComp {
self.scope.destroy_boxed(parent_to_detach);
}

fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
self.scope.shift_node(next_parent.clone(), next_sibling);

self.node_ref.clone()
}
}

Expand All @@ -62,20 +65,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 +123,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 +149,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
12 changes: 4 additions & 8 deletions packages/yew/src/dom_bundle/blist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,10 @@ impl ReconcileTarget for BList {
}
}

fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
let mut next_sibling = next_sibling;

for node in self.rev_children.iter() {
next_sibling = node.shift(next_parent, next_sibling.clone());
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
for node in self.rev_children.iter().rev() {
node.shift(next_parent, next_sibling.clone());
}

next_sibling
}
}

Expand Down Expand Up @@ -484,7 +480,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
4 changes: 1 addition & 3 deletions packages/yew/src/dom_bundle/bnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl ReconcileTarget for BNode {
}
}

fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
match self {
Self::Tag(ref vtag) => vtag.shift(next_parent, next_sibling),
Self::Text(ref btext) => btext.shift(next_parent, next_sibling),
Expand All @@ -72,8 +72,6 @@ impl ReconcileTarget for BNode {
next_parent
.insert_before(node, next_sibling.get().as_ref())
.unwrap();

NodeRef::new(node.clone())
}
Self::Portal(ref vportal) => vportal.shift(next_parent, next_sibling),
Self::Suspense(ref vsuspense) => vsuspense.shift(next_parent, next_sibling),
Expand Down
4 changes: 1 addition & 3 deletions packages/yew/src/dom_bundle/bportal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ impl ReconcileTarget for BPortal {
self.node.detach(&self.inner_root, &self.host, false);
}

fn shift(&self, _next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
fn shift(&self, _next_parent: &Element, _next_sibling: NodeRef) {
// portals have nothing in it's original place of DOM, we also do nothing.

next_sibling
}
}

Expand Down
14 changes: 10 additions & 4 deletions packages/yew/src/dom_bundle/bsuspense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ impl ReconcileTarget for BSuspense {
}
}

fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
match self.fallback.as_ref() {
Some(Fallback::Bundle(bundle)) => bundle.shift(next_parent, next_sibling),
Some(Fallback::Bundle(bundle)) => {
bundle.shift(next_parent, next_sibling);
}
#[cfg(feature = "hydration")]
Some(Fallback::Fragment(fragment)) => fragment.shift(next_parent, next_sibling),
None => self.children_bundle.shift(next_parent, next_sibling),
Some(Fallback::Fragment(fragment)) => {
fragment.shift(next_parent, next_sibling);
}
None => {
self.children_bundle.shift(next_parent, next_sibling);
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/yew/src/dom_bundle/btag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@ impl ReconcileTarget for BTag {
}
}

fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
next_parent
.insert_before(&self.reference, next_sibling.get().as_ref())
.unwrap();

self.node_ref.clone()
}
}

Expand Down
4 changes: 1 addition & 3 deletions packages/yew/src/dom_bundle/btext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ impl ReconcileTarget for BText {
}
}

fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
let node = &self.text_node;

next_parent
.insert_before(node, next_sibling.get().as_ref())
.unwrap();

NodeRef::new(self.text_node.clone().into())
}
}

Expand Down
7 changes: 1 addition & 6 deletions packages/yew/src/dom_bundle/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,11 @@ impl Fragment {
}

/// Shift current Fragment into a different position in the dom.
pub fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
pub fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
for node in self.iter() {
next_parent
.insert_before(node, next_sibling.get().as_ref())
.unwrap();
}

self.front()
.cloned()
.map(NodeRef::new)
.unwrap_or(next_sibling)
}
}
2 changes: 1 addition & 1 deletion packages/yew/src/dom_bundle/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(super) trait ReconcileTarget {
/// Move elements from one parent to another parent.
/// This is for example used by `VSuspense` to preserve component state without detaching
/// (which destroys component state).
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef;
fn shift(&self, next_parent: &Element, next_sibling: NodeRef);
}

/// This trait provides features to update a tree by calculating a difference against another tree.
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

0 comments on commit f1e4089

Please sign in to comment.