Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an internal DomSlot for positioning instead of NodeRef #3048

Merged
merged 22 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions packages/yew/src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use std::rc::Rc;

use web_sys::Element;

use crate::dom_bundle::BSubtree;
use crate::html::{BaseComponent, NodeRef, Scope, Scoped};
use crate::dom_bundle::{BSubtree, DomSlot, DynamicDomSlot};
use crate::html::{BaseComponent, Scope, Scoped};

/// An instance of an application.
#[cfg(feature = "csr")]
#[derive(Debug)]
pub struct AppHandle<COMP: BaseComponent> {
/// `Scope` holder
Expand Down Expand Up @@ -38,8 +37,8 @@ where
app.scope.mount_in_place(
hosting_root,
host,
NodeRef::default(),
NodeRef::default(),
DomSlot::at_end(),
DynamicDomSlot::new_debug_trapped(),
props,
);

Expand All @@ -58,7 +57,7 @@ where
skip_all,
)]
pub fn update(&mut self, new_props: COMP::Properties) {
self.scope.reuse(Rc::new(new_props), NodeRef::default())
self.scope.reuse(Rc::new(new_props), DomSlot::at_end())
}

/// Schedule the app for destruction
Expand Down Expand Up @@ -115,11 +114,11 @@ mod feat_hydration {
hosting_root,
host.clone(),
&mut fragment,
NodeRef::default(),
DynamicDomSlot::new_debug_trapped(),
Rc::clone(&props),
);
#[cfg(debug_assertions)] // Fix trapped next_sibling at the root
app.scope.reuse(props, NodeRef::default());
app.scope.reuse(props, DomSlot::at_end());

// We remove all remaining nodes, this mimics the clear_element behaviour in
// mount_with_props.
Expand Down
70 changes: 33 additions & 37 deletions packages/yew/src/dom_bundle/bcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ use std::fmt;

use web_sys::Element;

use super::{BNode, BSubtree, Reconcilable, ReconcileTarget};
use super::{BNode, BSubtree, DomSlot, DynamicDomSlot, Reconcilable, ReconcileTarget};
use crate::html::{AnyScope, Scoped};
use crate::virtual_dom::{Key, VComp};
use crate::NodeRef;

/// A virtual component. Compare with [VComp].
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,
/// An internal [`DomSlot`] passed around to track this components position. This
/// will dynamically adjust when a lifecycle changes the render state of this component.
own_position: DynamicDomSlot,
key: Option<Key>,
}

Expand All @@ -41,10 +40,10 @@ impl ReconcileTarget for BComp {
self.scope.destroy_boxed(parent_to_detach);
}

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

self.internal_ref.clone()
self.own_position.to_position()
}
}

Expand All @@ -56,29 +55,29 @@ impl Reconcilable for VComp {
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
) -> (NodeRef, Self::Bundle) {
slot: DomSlot,
) -> (DomSlot, Self::Bundle) {
let VComp {
type_id,
mountable,
key,
..
} = self;
let internal_ref = NodeRef::default();
let internal_ref = DynamicDomSlot::new_debug_trapped();

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

(
internal_ref.clone(),
internal_ref.to_position(),
BComp {
type_id,
internal_ref,
own_position: internal_ref,
key,
scope,
},
Expand All @@ -90,17 +89,17 @@ impl Reconcilable for VComp {
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
slot: DomSlot,
bundle: &mut BNode,
) -> NodeRef {
) -> DomSlot {
match bundle {
// If the existing bundle is the same type, reuse it and update its properties
BNode::Comp(ref mut bcomp)
if self.type_id == bcomp.type_id && self.key == bcomp.key =>
{
self.reconcile(root, parent_scope, parent, next_sibling, bcomp)
self.reconcile(root, parent_scope, parent, slot, bcomp)
}
_ => self.replace(root, parent_scope, parent, next_sibling, bundle),
_ => self.replace(root, parent_scope, parent, slot, bundle),
}
}

Expand All @@ -109,14 +108,14 @@ impl Reconcilable for VComp {
_root: &BSubtree,
_parent_scope: &AnyScope,
_parent: &Element,
next_sibling: NodeRef,
slot: DomSlot,
bcomp: &mut Self::Bundle,
) -> NodeRef {
) -> DomSlot {
let VComp { mountable, key, .. } = self;

bcomp.key = key;
mountable.reuse(bcomp.scope.borrow(), next_sibling);
bcomp.internal_ref.clone()
mountable.reuse(bcomp.scope.borrow(), slot);
bcomp.own_position.to_position()
}
}

Expand All @@ -132,14 +131,14 @@ mod feat_hydration {
parent_scope: &AnyScope,
parent: &Element,
fragment: &mut Fragment,
) -> (NodeRef, Self::Bundle) {
) -> Self::Bundle {
let VComp {
type_id,
mountable,
key,
..
} = self;
let internal_ref = NodeRef::default();
let internal_ref = DynamicDomSlot::new_debug_trapped();

let scoped = mountable.hydrate(
root.clone(),
Expand All @@ -149,15 +148,12 @@ mod feat_hydration {
fragment,
);

(
internal_ref.clone(),
BComp {
type_id,
scope: scoped,
internal_ref,
key,
},
)
BComp {
type_id,
scope: scoped,
own_position: internal_ref,
key,
}
}
}
}
Expand All @@ -172,7 +168,7 @@ mod tests {
use super::*;
use crate::dom_bundle::Reconcilable;
use crate::virtual_dom::{Key, VChild, VNode};
use crate::{html, scheduler, Children, Component, Context, Html, NodeRef, Properties};
use crate::{html, scheduler, Children, Component, Context, Html, Properties};

wasm_bindgen_test_configure!(run_in_browser);

Expand Down Expand Up @@ -208,12 +204,12 @@ mod tests {
let (root, scope, parent) = setup_parent();

let comp = html! { <Comp></Comp> };
let (_, mut bundle) = comp.attach(&root, &scope, &parent, NodeRef::default());
let (_, mut bundle) = comp.attach(&root, &scope, &parent, DomSlot::at_end());
scheduler::start_now();

for _ in 0..10000 {
let node = html! { <Comp></Comp> };
node.reconcile_node(&root, &scope, &parent, NodeRef::default(), &mut bundle);
node.reconcile_node(&root, &scope, &parent, DomSlot::at_end(), &mut bundle);
scheduler::start_now();
}
}
Expand Down Expand Up @@ -344,7 +340,7 @@ mod tests {
// clear parent
parent.set_inner_html("");

node.attach(root, scope, parent, NodeRef::default());
node.attach(root, scope, parent, DomSlot::at_end());
scheduler::start_now();
parent.inner_html()
}
Expand Down
Loading