convert T into signals automatically

This commit is contained in:
Evan Almloff committed 2024-01-29 13:36:39 -06:00
1 parent 1847c737e9
commit 50e3216d8b
14 files changed
+441 -103

No files matched your search

+5 -5
View File
@@ -8,7 +8,7 @@ pub(crate) trait AnyProps: 'static {
/// Render the component with the internal props.
fn render(&self) -> RenderReturn;
/// Check if the props are the same as the type erased props of another component.
fn memoize(&self, other: &dyn Any) -> bool;
fn memoize(&mut self, other: &dyn Any) -> bool;
/// Get the props as a type erased `dyn Any`.
fn props(&self) -> &dyn Any;
/// Duplicate this component into a new boxed component.
@@ -18,7 +18,7 @@ pub(crate) trait AnyProps: 'static {
/// A component along with the props the component uses to render.
pub(crate) struct VProps<F: ComponentFunction<P, M>, P, M> {
render_fn: F,
memo: fn(&P, &P) -> bool,
memo: fn(&mut P, &P) -> bool,
props: P,
name: &'static str,
phantom: std::marker::PhantomData<M>,
@@ -40,7 +40,7 @@ impl<F: ComponentFunction<P, M> + Clone, P: Clone + 'static, M: 'static> VProps<
/// Create a [`VProps`] object.
pub fn new(
render_fn: F,
memo: fn(&P, &P) -> bool,
memo: fn(&mut P, &P) -> bool,
props: P,
name: &'static str,
) -> VProps<F, P, M> {
@@ -57,9 +57,9 @@ impl<F: ComponentFunction<P, M> + Clone, P: Clone + 'static, M: 'static> VProps<
impl<F: ComponentFunction<P, M> + Clone, P: Clone + 'static, M: 'static> AnyProps
for VProps<F, P, M>
{
fn memoize(&self, other: &dyn Any) -> bool {
fn memoize(&mut self, other: &dyn Any) -> bool {
match other.downcast_ref::<P>() {
Some(other) => (self.memo)(&self.props, other),
Some(other) => (self.memo)(&mut self.props, other),
None => false,
}
}
+2 -2
View File
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
use crate::{
any_props::AnyProps,
@@ -72,7 +72,7 @@ impl VNode {
// copy out the box for both
let old_scope = &mut dom.scopes[scope_id.0];
let old_props: &dyn AnyProps = old_scope.props.deref();
let old_props: &mut dyn AnyProps = old_scope.props.deref_mut();
let new_props: &dyn AnyProps = new.props.deref();
// If the props are static, then we try to memoize by setting the new with the old
+1 -1
View File
@@ -330,7 +330,7 @@ impl Properties for ErrorBoundaryProps {
fn builder() -> Self::Builder {
ErrorBoundaryProps::builder()
}
fn memoize(&self, _: &Self) -> bool {
fn memoize(&mut self, _: &Self) -> bool {
false
}
}
+1 -1
View File
@@ -90,7 +90,7 @@ impl Properties for FragmentProps {
fn builder() -> Self::Builder {
FragmentBuilder(None)
}
fn memoize(&self, _other: &Self) -> bool {
fn memoize(&mut self, _other: &Self) -> bool {
false
}
}
+12 -3
View File
@@ -32,7 +32,16 @@ pub trait Properties: Clone + Sized + 'static {
fn builder() -> Self::Builder;
/// Compare two props to see if they are memoizable.
fn memoize(&self, other: &Self) -> bool;
fn memoize(&mut self, other: &Self) -> bool;
/// Create a component from the props.
fn into_vcomponent<M: 'static>(
self,
render_fn: impl ComponentFunction<Self, M>,
component_name: &'static str,
) -> VComponent {
VComponent::new(render_fn, self, component_name)
}
}
impl Properties for () {
@@ -40,7 +49,7 @@ impl Properties for () {
fn builder() -> Self::Builder {
EmptyBuilder {}
}
fn memoize(&self, _other: &Self) -> bool {
fn memoize(&mut self, _other: &Self) -> bool {
true
}
}
@@ -65,7 +74,7 @@ where
fn builder() -> Self::Builder {
todo!()
}
fn memoize(&self, _other: &Self) -> bool {
fn memoize(&mut self, _other: &Self) -> bool {
true
}
}