Skip to content

Commit

Permalink
improve code size
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldSEnder committed Mar 26, 2022
1 parent 8499e8f commit aa85eca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/yew/src/html/component/scope.rs
Expand Up @@ -392,12 +392,12 @@ mod feat_csr {
ComponentRenderState, CreateRunner, DestroyRunner, RenderRunner,
};
use crate::html::{CompAnyRef, NodeRef};
use crate::{scheduler, ComponentRef};
use crate::scheduler;
use std::cell::Ref;
use web_sys::Element;

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

Expand Down Expand Up @@ -484,7 +484,7 @@ mod feat_csr {
self.scope.clone().into()
}
fn scope_ref(&self) -> CompAnyRef {
self.scope_ref.to_any()
self.scope_ref.clone()
}
fn render_state(&self) -> Option<Ref<'_, dyn fmt::Debug>> {
let state_ref = self.scope.state.borrow();
Expand Down
14 changes: 9 additions & 5 deletions packages/yew/src/html/mod.rs
Expand Up @@ -172,6 +172,7 @@ mod feat_csr {
}
}

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

impl<COMP: BaseComponent> PartialEq<ComponentRef<COMP>> for CompAnyRef {
Expand All @@ -189,15 +190,18 @@ mod feat_csr {
}

impl<COMP: BaseComponent> ComponentRef<COMP> {
/// Place a scope in a reference for later use
pub(crate) fn set(&self, scope: Option<Scope<COMP>>) {
let mut this = self.0.borrow_mut();
this.scope = scope.map(|s| s.into());
}
/// 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>) {
let mut this = self.0.borrow_mut();
this.scope = scope;
}
/// 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
Expand Down
23 changes: 13 additions & 10 deletions packages/yew/src/virtual_dom/vcomp.rs
Expand Up @@ -12,7 +12,7 @@ use crate::html::{AnyScope, Scope};
#[cfg(feature = "csr")]
use crate::dom_bundle::BSubtree;
#[cfg(feature = "csr")]
use crate::html::{NodeRef, Scoped};
use crate::html::{CompAnyRef, NodeRef, Scoped};
#[cfg(feature = "csr")]
use web_sys::Element;

Expand Down Expand Up @@ -75,20 +75,23 @@ pub(crate) trait Mountable {

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

impl<COMP: BaseComponent> PropsWrapper<COMP> {
pub fn new(props: Rc<COMP::Properties>, comp_ref: ComponentRef<COMP>) -> Self {
Self { props, comp_ref }
pub fn new(props: Rc<COMP::Properties>, external_ref: ComponentRef<COMP>) -> Self {
Self {
props,
external_ref: external_ref.to_any(),
}
}
}

impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
fn copy(&self) -> Box<dyn Mountable> {
let wrapper: PropsWrapper<COMP> = PropsWrapper {
props: Rc::clone(&self.props),
comp_ref: self.comp_ref.clone(),
external_ref: self.external_ref.clone(),
};
Box::new(wrapper)
}
Expand All @@ -105,20 +108,20 @@ 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);
self.comp_ref.set(Some(scope.clone()));
self.external_ref.set(Some(scope.clone().into()));

Box::new(crate::html::ScopeHolder {
scope,
scope_ref: self.comp_ref,
scope_ref: self.external_ref,
})
}

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

#[cfg(feature = "ssr")]
Expand All @@ -136,7 +139,7 @@ impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {

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

Expand Down

0 comments on commit aa85eca

Please sign in to comment.