Remove boxedanyprops abstraction
This commit is contained in:
4 files changed
+9
-37
No files matched your search
@@ -1,41 +1,14 @@
|
|||||||
use crate::{nodes::RenderReturn, Component};
|
use crate::{nodes::RenderReturn, Component};
|
||||||
use std::{any::Any, ops::Deref, panic::AssertUnwindSafe};
|
use std::{any::Any, panic::AssertUnwindSafe};
|
||||||
|
|
||||||
/// A boxed version of AnyProps that can be cloned
|
pub type BoxedAnyProps = Box<dyn AnyProps>;
|
||||||
pub(crate) struct BoxedAnyProps {
|
|
||||||
inner: Box<dyn AnyProps>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BoxedAnyProps {
|
|
||||||
pub fn new(inner: impl AnyProps + 'static) -> Self {
|
|
||||||
Self {
|
|
||||||
inner: Box::new(inner),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for BoxedAnyProps {
|
|
||||||
type Target = dyn AnyProps;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&*self.inner
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clone for BoxedAnyProps {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
inner: self.inner.duplicate(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A trait that essentially allows VComponentProps to be used generically
|
/// A trait that essentially allows VComponentProps to be used generically
|
||||||
pub(crate) trait AnyProps {
|
pub(crate) trait AnyProps {
|
||||||
fn render(&self) -> RenderReturn;
|
fn render(&self) -> RenderReturn;
|
||||||
fn memoize(&self, other: &dyn Any) -> bool;
|
fn memoize(&self, other: &dyn Any) -> bool;
|
||||||
fn props(&self) -> &dyn Any;
|
fn props(&self) -> &dyn Any;
|
||||||
fn duplicate(&self) -> Box<dyn AnyProps>;
|
fn duplicate(&self) -> BoxedAnyProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct VProps<P: 'static> {
|
pub(crate) struct VProps<P: 'static> {
|
||||||
@@ -75,7 +48,6 @@ impl<P: Clone + 'static> AnyProps for VProps<P> {
|
|||||||
|
|
||||||
fn render(&self) -> RenderReturn {
|
fn render(&self) -> RenderReturn {
|
||||||
let res = std::panic::catch_unwind(AssertUnwindSafe(move || {
|
let res = std::panic::catch_unwind(AssertUnwindSafe(move || {
|
||||||
// Call the render function directly
|
|
||||||
(self.render_fn)(self.props.clone())
|
(self.render_fn)(self.props.clone())
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -90,7 +62,7 @@ impl<P: Clone + 'static> AnyProps for VProps<P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn duplicate(&self) -> Box<dyn AnyProps> {
|
fn duplicate(&self) -> BoxedAnyProps {
|
||||||
Box::new(Self {
|
Box::new(Self {
|
||||||
render_fn: self.render_fn.clone(),
|
render_fn: self.render_fn.clone(),
|
||||||
memo: self.memo,
|
memo: self.memo,
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ impl VNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// First, move over the props from the old to the new, dropping old props in the process
|
// First, move over the props from the old to the new, dropping old props in the process
|
||||||
dom.scopes[scope_id.0].props = new.props.clone();
|
dom.scopes[scope_id.0].props = new.props.duplicate();
|
||||||
|
|
||||||
// Now run the component and diff it
|
// Now run the component and diff it
|
||||||
let new = dom.run_scope(scope_id);
|
let new = dom.run_scope(scope_id);
|
||||||
@@ -129,7 +129,7 @@ impl VNode {
|
|||||||
) -> usize {
|
) -> usize {
|
||||||
// Load up a ScopeId for this vcomponent. If it's already mounted, then we can just use that
|
// Load up a ScopeId for this vcomponent. If it's already mounted, then we can just use that
|
||||||
let scope = dom
|
let scope = dom
|
||||||
.new_scope(component.props.clone(), component.name)
|
.new_scope(component.props.duplicate(), component.name)
|
||||||
.context()
|
.context()
|
||||||
.id;
|
.id;
|
||||||
|
|
||||||
|
|||||||
@@ -549,7 +549,7 @@ impl VComponent {
|
|||||||
|
|
||||||
VComponent {
|
VComponent {
|
||||||
name: fn_name,
|
name: fn_name,
|
||||||
props: BoxedAnyProps::new(vcomp),
|
props: Box::new(vcomp),
|
||||||
render_fn,
|
render_fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
//! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
|
//! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
any_props::{BoxedAnyProps, VProps},
|
any_props::VProps,
|
||||||
arena::ElementId,
|
arena::ElementId,
|
||||||
innerlude::{
|
innerlude::{
|
||||||
DirtyScope, ElementRef, ErrorBoundary, NoOpMutations, SchedulerMsg, ScopeState, VNodeMount,
|
DirtyScope, ElementRef, ErrorBoundary, NoOpMutations, SchedulerMsg, ScopeState, VNodeMount,
|
||||||
@@ -287,7 +287,7 @@ impl VirtualDom {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let root = dom.new_scope(
|
let root = dom.new_scope(
|
||||||
BoxedAnyProps::new(VProps::new(
|
Box::new(VProps::new(
|
||||||
Rc::new(root).as_component(),
|
Rc::new(root).as_component(),
|
||||||
|_, _| true,
|
|_, _| true,
|
||||||
root_props,
|
root_props,
|
||||||
|
|||||||
Reference in new issue
Block a user