Skip to content

Commit

Permalink
code-size try 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldSEnder committed Mar 26, 2022
1 parent 4ace53c commit a6526fb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 deletions.
8 changes: 2 additions & 6 deletions examples/nested_list/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ where
impl From<ListVariant> for Html {
fn from(variant: ListVariant) -> Html {
match variant.props {
Variants::Header(props) => {
VComp::new::<ListHeader>(props, ComponentRef::default(), None).into()
}
Variants::Item(props) => {
VComp::new::<ListItem>(props, ComponentRef::default(), None).into()
}
Variants::Header(props) => VComp::new::<ListHeader>(props, None, None).into(),
Variants::Item(props) => VComp::new::<ListItem>(props, None, None).into(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/yew-macro/src/html_tree/html_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ impl ToTokens for HtmlComponent {
let node_ref = if let Some(node_ref) = &special_props.node_ref {
let value = &node_ref.value;
let value_quoted = quote_spanned! {value.span()=> #value };
quote! { ::yew::html::IntoPropValue::< #comp_ref_ty >::into_prop_value( #value_quoted ) }
quote! { Some(::yew::html::IntoPropValue::< #comp_ref_ty >::into_prop_value( #value_quoted )) }
} else {
quote_spanned! {ty.span()=> < #comp_ref_ty as ::std::default::Default>::default() }
quote_spanned! {ty.span()=> ::std::option::Option::None }
};

let key = if let Some(key) = &special_props.key {
Expand Down
8 changes: 4 additions & 4 deletions packages/yew/src/dom_bundle/bcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mod tests {
VNode::VComp(vcomp) => vcomp,
_ => unreachable!("should be a vcomp"),
};
assert_eq!(vcomp.mountable.component_ref(), test_node_ref);
assert_eq!(vcomp.mountable.component_ref().unwrap(), test_node_ref);
};

let props = Props {
Expand All @@ -257,7 +257,7 @@ mod tests {
field_1: 1,
field_2: 1,
},
ComponentRef::default(),
None,
None,
);

Expand All @@ -266,7 +266,7 @@ mod tests {
field_1: 1,
field_2: 1,
},
ComponentRef::default(),
None,
None,
);

Expand All @@ -275,7 +275,7 @@ mod tests {
field_1: 2,
field_2: 2,
},
ComponentRef::default(),
None,
None,
);

Expand Down
10 changes: 6 additions & 4 deletions packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ mod feat_csr {
use web_sys::Element;

pub(crate) struct ScopeHolder<COMP: BaseComponent> {
pub scope_ref: CompAnyRef,
pub scope: Scope<COMP>,
pub scope_ref: Option<CompAnyRef>,
}

impl<COMP: BaseComponent> fmt::Debug for ScopeHolder<COMP> {
Expand Down Expand Up @@ -470,7 +470,7 @@ mod feat_csr {

pub(crate) trait Scoped {
fn to_any(&self) -> AnyScope;
fn scope_ref(&self) -> CompAnyRef;
fn scope_ref(&self) -> Option<CompAnyRef>;
/// Get the render state if it hasn't already been destroyed
fn render_state(&self) -> Option<Ref<'_, dyn fmt::Debug>>;
/// Shift the node associated with this scope to a new place
Expand All @@ -483,7 +483,7 @@ mod feat_csr {
fn to_any(&self) -> AnyScope {
self.scope.clone().into()
}
fn scope_ref(&self) -> CompAnyRef {
fn scope_ref(&self) -> Option<CompAnyRef> {
self.scope_ref.clone()
}
fn render_state(&self) -> Option<Ref<'_, dyn fmt::Debug>> {
Expand All @@ -498,7 +498,9 @@ mod feat_csr {
}
/// Process an event to destroy a component
fn destroy_boxed(self: Box<Self>, parent_to_detach: bool) {
self.scope_ref.set(None);
if let Some(scope_ref) = self.scope_ref {
scope_ref.set(None);
}
self.scope.destroy(parent_to_detach);
}
fn shift_node(&self, parent: Element, next_sibling: NodeRef) {
Expand Down
29 changes: 16 additions & 13 deletions packages/yew/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ mod feat_csr {
}
}

#[derive(Clone)]
pub(crate) struct CompAnyRef(Rc<RefCell<CompRefInner>>);

impl<COMP: BaseComponent> PartialEq<ComponentRef<COMP>> for CompAnyRef {
fn eq(&self, other: &ComponentRef<COMP>) -> bool {
Rc::ptr_eq(&self.0, &other.0)
Expand All @@ -189,13 +186,6 @@ mod feat_csr {
}
}

impl<COMP: BaseComponent> ComponentRef<COMP> {
/// Convert to type erased, inner form
pub(crate) fn to_any(&self) -> CompAnyRef {
CompAnyRef(self.0.clone())
}
}

impl CompAnyRef {
/// Place a scope in a reference for later use
pub(crate) fn set(&self, scope: Option<AnyScope>) {
Expand All @@ -205,16 +195,18 @@ mod feat_csr {
/// Re-use the old inner reference.
/// MUST BE SURE THAT THE COMPONENT TYPES MATCH
/// for example, by downcasting a Scope of the same type first
pub(crate) fn reuse_any(&self, old_ref: &CompAnyRef) {
pub(crate) fn reuse_any(&self, old_ref: Option<CompAnyRef>) {
let old_ref = match old_ref {
Some(val) => val,
None => return,
};
if Rc::ptr_eq(&self.0, &old_ref.0) {
return;
}
self.0.borrow_mut().scope = old_ref.0.borrow_mut().scope.take();
}
}
}
#[cfg(feature = "csr")]
pub(crate) use feat_csr::*;

/// Wrapped reference to another component for later use in lifecycle methods.
///
Expand Down Expand Up @@ -309,6 +301,17 @@ impl<COMP: BaseComponent> std::fmt::Debug for ComponentRef<COMP> {
}
}

#[derive(Clone)]
pub(crate) struct CompAnyRef(Rc<RefCell<CompRefInner>>);

impl<COMP: BaseComponent> ComponentRef<COMP> {
#[inline]
/// Convert to type erased, inner form
pub(crate) fn to_any(&self) -> CompAnyRef {
CompAnyRef(self.0.clone())
}
}

#[derive(Default)]
struct CompRefInner {
scope: Option<AnyScope>,
Expand Down
36 changes: 21 additions & 15 deletions packages/yew/src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains the implementation of a virtual component (`VComp`).

use super::Key;
use crate::html::{BaseComponent, ComponentRef, IntoComponent};
use crate::html::{BaseComponent, CompAnyRef, ComponentRef, IntoComponent};
use std::any::TypeId;
use std::fmt;
use std::rc::Rc;
Expand Down Expand Up @@ -70,19 +70,19 @@ pub(crate) trait Mountable {
) -> LocalBoxFuture<'a, ()>;

#[cfg(all(test, feature = "csr"))]
fn component_ref(&self) -> crate::html::CompAnyRef;
fn component_ref(&self) -> Option<CompAnyRef>;
}

pub(crate) struct PropsWrapper<COMP: BaseComponent> {
props: Rc<COMP::Properties>,
external_ref: ComponentRef<COMP>,
external_ref: Option<CompAnyRef>,
}

impl<COMP: BaseComponent> PropsWrapper<COMP> {
pub fn new(props: Rc<COMP::Properties>, external_ref: ComponentRef<COMP>) -> Self {
pub fn new(props: Rc<COMP::Properties>, external_ref: Option<ComponentRef<COMP>>) -> Self {
Self {
props,
external_ref,
external_ref: external_ref.map(|r| r.to_any()),
}
}
}
Expand All @@ -108,19 +108,25 @@ impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
let scope: Scope<COMP> = Scope::new(Some(parent_scope.clone()));

scope.mount_in_place(root.clone(), parent, next_sibling, internal_ref, self.props);
let scope_ref = self.external_ref.to_any();
scope_ref.set(Some(scope.clone().into()));
let scope_ref = self.external_ref;
if let Some(ref scope_ref) = scope_ref {
scope_ref.set(Some(scope.clone().into()));
}

Box::new(crate::html::ScopeHolder { scope, scope_ref })
}

#[cfg(feature = "csr")]
fn reuse(self: Box<Self>, scoped: &dyn Scoped, next_sibling: NodeRef) {
let scope: Scope<COMP> = scoped.to_any().downcast::<COMP>();
let scope_ref = self.external_ref.to_any();
scope_ref.reuse_any(&scoped.scope_ref());
let scope_ref = self.external_ref;
if let Some(ref scope_ref) = scope_ref {
scope_ref.reuse_any(scoped.scope_ref());
}
scope.reuse(self.props, next_sibling);
scope_ref.set(Some(scope.into()));
if let Some(ref scope_ref) = scope_ref {
scope_ref.set(Some(scope.into()));
}
}

#[cfg(feature = "ssr")]
Expand All @@ -137,8 +143,8 @@ impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
}

#[cfg(all(test, feature = "csr"))]
fn component_ref(&self) -> crate::html::CompAnyRef {
self.external_ref.to_any()
fn component_ref(&self) -> Option<CompAnyRef> {
self.external_ref.clone()
}
}

Expand All @@ -147,7 +153,7 @@ pub struct VChild<ICOMP: IntoComponent> {
/// The component properties
pub props: Rc<ICOMP::Properties>,
/// Reference to the mounted node
comp_ref: ComponentRef<ICOMP::Component>,
comp_ref: Option<ComponentRef<ICOMP::Component>>,
key: Option<Key>,
}

Expand Down Expand Up @@ -177,7 +183,7 @@ where
/// Creates a child component that can be accessed and modified by its parent.
pub fn new(
props: ICOMP::Properties,
comp_ref: ComponentRef<ICOMP::Component>,
comp_ref: Option<ComponentRef<ICOMP::Component>>,
key: Option<Key>,
) -> Self {
Self {
Expand All @@ -201,7 +207,7 @@ impl VComp {
/// Creates a new `VComp` instance.
pub fn new<ICOMP>(
props: Rc<ICOMP::Properties>,
comp_ref: ComponentRef<ICOMP::Component>,
comp_ref: Option<ComponentRef<ICOMP::Component>>,
key: Option<Key>,
) -> Self
where
Expand Down

0 comments on commit a6526fb

Please sign in to comment.