try not double boxing
This commit is contained in:
1 parent
0c532c5e0c
commit
dab87c0144
11 files changed
+166
-103
No files matched your search
@@ -1,7 +1,7 @@
|
||||
use crate::{nodes::RenderReturn, Component};
|
||||
use crate::{nodes::RenderReturn, ComponentFunction};
|
||||
use std::{any::Any, panic::AssertUnwindSafe};
|
||||
|
||||
pub type BoxedAnyProps = Box<dyn AnyProps>;
|
||||
pub(crate) type BoxedAnyProps = Box<dyn AnyProps>;
|
||||
|
||||
/// A trait that essentially allows VComponentProps to be used generically
|
||||
pub(crate) trait AnyProps {
|
||||
@@ -12,8 +12,8 @@ pub(crate) trait AnyProps {
|
||||
}
|
||||
|
||||
/// Create a new boxed props object.
|
||||
pub fn new_any_props<P: 'static + Clone>(
|
||||
render_fn: Component<P>,
|
||||
pub(crate) fn new_any_props<F: ComponentFunction<P, M>, P: Clone + 'static, M: 'static>(
|
||||
render_fn: F,
|
||||
memo: fn(&P, &P) -> bool,
|
||||
props: P,
|
||||
name: &'static str,
|
||||
@@ -23,17 +23,21 @@ pub fn new_any_props<P: 'static + Clone>(
|
||||
memo,
|
||||
props,
|
||||
name,
|
||||
phantom: std::marker::PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
struct VProps<P> {
|
||||
render_fn: Component<P>,
|
||||
struct VProps<F: ComponentFunction<P, M>, P, M> {
|
||||
render_fn: F,
|
||||
memo: fn(&P, &P) -> bool,
|
||||
props: P,
|
||||
name: &'static str,
|
||||
phantom: std::marker::PhantomData<M>,
|
||||
}
|
||||
|
||||
impl<P: Clone + 'static> AnyProps for VProps<P> {
|
||||
impl<F: ComponentFunction<P, M> + Clone, P: Clone + 'static, M: 'static> AnyProps
|
||||
for VProps<F, P, M>
|
||||
{
|
||||
fn memoize(&self, other: &dyn Any) -> bool {
|
||||
match other.downcast_ref::<P>() {
|
||||
Some(other) => (self.memo)(&self.props, other),
|
||||
@@ -47,7 +51,7 @@ impl<P: Clone + 'static> AnyProps for VProps<P> {
|
||||
|
||||
fn render(&self) -> RenderReturn {
|
||||
let res = std::panic::catch_unwind(AssertUnwindSafe(move || {
|
||||
(self.render_fn)(self.props.clone())
|
||||
self.render_fn.rebuild(self.props.clone())
|
||||
}));
|
||||
|
||||
match res {
|
||||
@@ -67,6 +71,7 @@ impl<P: Clone + 'static> AnyProps for VProps<P> {
|
||||
memo: self.memo,
|
||||
props: self.props.clone(),
|
||||
name: self.name,
|
||||
phantom: std::marker::PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -520,7 +520,7 @@ pub struct VComponent {
|
||||
|
||||
impl VComponent {
|
||||
/// Create a new [`VComponent`] variant
|
||||
pub fn new<P, M>(
|
||||
pub fn new<P, M: 'static>(
|
||||
component: impl ComponentFunction<P, M>,
|
||||
props: P,
|
||||
fn_name: &'static str,
|
||||
@@ -528,9 +528,7 @@ impl VComponent {
|
||||
where
|
||||
P: Properties + 'static,
|
||||
{
|
||||
let component = Rc::new(component);
|
||||
let render_fn = component.id();
|
||||
let component = component.as_component();
|
||||
let props = new_any_props(component, <P as Properties>::memoize, props, fn_name);
|
||||
|
||||
VComponent {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use std::any::Any;
|
||||
|
||||
use crate::{properties::ComponentFunction, Component, VirtualDom};
|
||||
use crate::{
|
||||
properties::{ComponentFunction, RootProps},
|
||||
VComponent, VirtualDom,
|
||||
};
|
||||
|
||||
/// A boxed object that can be injected into a component's context.
|
||||
pub struct BoxedContext(Box<dyn ClonableAny>);
|
||||
@@ -40,32 +43,38 @@ impl<T: Any + Clone> ClonableAny for T {
|
||||
}
|
||||
|
||||
/// The platform-independent part of the config needed to launch an application.
|
||||
pub struct CrossPlatformConfig<Props: Clone + 'static> {
|
||||
pub struct CrossPlatformConfig {
|
||||
/// The root component function.
|
||||
pub component: Component<Props>,
|
||||
/// The props for the root component.
|
||||
pub props: Props,
|
||||
component: VComponent,
|
||||
/// The contexts to provide to the root component.
|
||||
pub root_contexts: Vec<BoxedContext>,
|
||||
root_contexts: Vec<BoxedContext>,
|
||||
}
|
||||
|
||||
impl<Props: Clone + 'static> CrossPlatformConfig<Props> {
|
||||
impl CrossPlatformConfig {
|
||||
/// Create a new cross-platform config.
|
||||
pub fn new<M>(
|
||||
pub fn new<Props: Clone + 'static, M: 'static>(
|
||||
component: impl ComponentFunction<Props, M>,
|
||||
props: Props,
|
||||
root_contexts: Vec<BoxedContext>,
|
||||
) -> Self {
|
||||
Self {
|
||||
component: ComponentFunction::as_component(std::rc::Rc::new(component)),
|
||||
props,
|
||||
component: VComponent::new(
|
||||
move |props: RootProps<Props>| component.rebuild(props.0),
|
||||
RootProps(props),
|
||||
"root",
|
||||
),
|
||||
root_contexts,
|
||||
}
|
||||
}
|
||||
|
||||
/// Push a new context into the root component's context.
|
||||
pub fn push_context<T: Any + Clone + 'static>(&mut self, context: T) {
|
||||
self.root_contexts.push(BoxedContext::new(context));
|
||||
}
|
||||
|
||||
/// Build a virtual dom from the config.
|
||||
pub fn build_vdom(self) -> VirtualDom {
|
||||
let mut vdom = VirtualDom::new_with_props(self.component, self.props);
|
||||
let mut vdom = VirtualDom::new_with_component(self.component);
|
||||
|
||||
for context in self.root_contexts {
|
||||
vdom.insert_boxed_root_context(context);
|
||||
@@ -76,18 +85,18 @@ impl<Props: Clone + 'static> CrossPlatformConfig<Props> {
|
||||
}
|
||||
|
||||
/// A builder to launch a specific platform.
|
||||
pub trait PlatformBuilder<Props: Clone + 'static> {
|
||||
pub trait PlatformBuilder {
|
||||
/// The platform-specific config needed to launch an application.
|
||||
type Config: Default;
|
||||
|
||||
/// Launch the app.
|
||||
fn launch(config: CrossPlatformConfig<Props>, platform_config: Self::Config);
|
||||
fn launch(config: CrossPlatformConfig, platform_config: Self::Config);
|
||||
}
|
||||
|
||||
impl<Props: Clone + 'static> PlatformBuilder<Props> for () {
|
||||
impl PlatformBuilder for () {
|
||||
type Config = ();
|
||||
|
||||
fn launch(_: CrossPlatformConfig<Props>, _: Self::Config) {
|
||||
fn launch(_: CrossPlatformConfig, _: Self::Config) {
|
||||
panic!("No platform is currently enabled. Please enable a platform feature for the dioxus crate.");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
use std::any::TypeId;
|
||||
|
||||
use crate::innerlude::*;
|
||||
|
||||
@@ -45,6 +45,31 @@ impl Properties for () {
|
||||
}
|
||||
}
|
||||
|
||||
/// Root properties never need to be memoized, so we can use a dummy implementation.
|
||||
pub(crate) struct RootProps<P>(pub P);
|
||||
|
||||
impl<P> Clone for RootProps<P>
|
||||
where
|
||||
P: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Properties for RootProps<P>
|
||||
where
|
||||
P: Clone + 'static,
|
||||
{
|
||||
type Builder = P;
|
||||
fn builder() -> Self::Builder {
|
||||
todo!()
|
||||
}
|
||||
fn memoize(&self, _other: &Self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
// We allow components to use the () generic parameter if they have no props. This impl enables the "build" method
|
||||
// that the macros use to anonymously complete prop construction.
|
||||
pub struct EmptyBuilder;
|
||||
@@ -62,35 +87,35 @@ where
|
||||
}
|
||||
|
||||
/// Any component that implements the `ComponentFn` trait can be used as a component.
|
||||
pub trait ComponentFunction<Props, Marker = ()>: 'static {
|
||||
pub trait ComponentFunction<Props, Marker = ()>: Clone + 'static {
|
||||
/// Get the type id of the component.
|
||||
fn id(&self) -> TypeId {
|
||||
TypeId::of::<Self>()
|
||||
}
|
||||
|
||||
/// Convert the component to a function that takes props and returns an element.
|
||||
fn as_component(self: Rc<Self>) -> Component<Props>;
|
||||
fn rebuild(&self, props: Props) -> Element;
|
||||
}
|
||||
|
||||
/// Accept pre-formed component render functions as components
|
||||
impl<P: 'static> ComponentFunction<P> for Component<P> {
|
||||
fn as_component(self: Rc<Self>) -> Component<P> {
|
||||
self.as_ref().clone()
|
||||
fn rebuild(&self, props: P) -> Element {
|
||||
(self)(props)
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept any callbacks that take props
|
||||
impl<F: Fn(P) -> Element + 'static, P> ComponentFunction<P> for F {
|
||||
fn as_component(self: Rc<Self>) -> Component<P> {
|
||||
self
|
||||
impl<F: Fn(P) -> Element + Clone + 'static, P> ComponentFunction<P> for F {
|
||||
fn rebuild(&self, props: P) -> Element {
|
||||
self(props)
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept any callbacks that take no props
|
||||
pub struct EmptyMarker;
|
||||
impl<F: Fn() -> Element + 'static> ComponentFunction<(), EmptyMarker> for F {
|
||||
fn as_component(self: Rc<Self>) -> Rc<dyn Fn(()) -> Element> {
|
||||
Rc::new(move |_| self())
|
||||
impl<F: Fn() -> Element + Clone + 'static> ComponentFunction<(), EmptyMarker> for F {
|
||||
fn rebuild(&self, _: ()) -> Element {
|
||||
self()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
//! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
|
||||
|
||||
use crate::{
|
||||
any_props::new_any_props,
|
||||
arena::ElementId,
|
||||
innerlude::{
|
||||
DirtyScope, ElementRef, ErrorBoundary, NoOpMutations, SchedulerMsg, ScopeState, VNodeMount,
|
||||
@@ -11,10 +10,10 @@ use crate::{
|
||||
},
|
||||
nodes::RenderReturn,
|
||||
nodes::{Template, TemplateId},
|
||||
properties::ComponentFunction,
|
||||
properties::RootProps,
|
||||
runtime::{Runtime, RuntimeGuard},
|
||||
scopes::ScopeId,
|
||||
AttributeValue, BoxedContext, Element, Event, Mutations, Task,
|
||||
AttributeValue, BoxedContext, ComponentFunction, Element, Event, Mutations, Task, VComponent,
|
||||
};
|
||||
use futures_util::{pin_mut, StreamExt};
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
@@ -230,13 +229,6 @@ impl VirtualDom {
|
||||
Self::new_with_props(app, ())
|
||||
}
|
||||
|
||||
/// Create a new virtualdom and build it immediately
|
||||
pub fn prebuilt(app: fn() -> Element) -> Self {
|
||||
let mut dom = Self::new(app);
|
||||
dom.rebuild_in_place();
|
||||
dom
|
||||
}
|
||||
|
||||
/// Create a new VirtualDom with the given properties for the root component.
|
||||
///
|
||||
/// # Description
|
||||
@@ -271,6 +263,53 @@ impl VirtualDom {
|
||||
root: impl ComponentFunction<P, M>,
|
||||
root_props: P,
|
||||
) -> Self {
|
||||
let vcomponent = VComponent::new(
|
||||
move |props: RootProps<P>| root.rebuild(props.0),
|
||||
RootProps(root_props),
|
||||
"root",
|
||||
);
|
||||
|
||||
Self::new_with_component(vcomponent)
|
||||
}
|
||||
|
||||
/// Create a new virtualdom and build it immediately
|
||||
pub fn prebuilt(app: fn() -> Element) -> Self {
|
||||
let mut dom = Self::new(app);
|
||||
dom.rebuild_in_place();
|
||||
dom
|
||||
}
|
||||
|
||||
/// Create a new VirtualDom with the given properties for the root component.
|
||||
///
|
||||
/// # Description
|
||||
///
|
||||
/// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
|
||||
///
|
||||
/// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
|
||||
/// to toss out the entire tree.
|
||||
///
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust, ignore
|
||||
/// #[derive(PartialEq, Props)]
|
||||
/// struct SomeProps {
|
||||
/// name: &'static str
|
||||
/// }
|
||||
///
|
||||
/// fn Example(cx: SomeProps) -> Element {
|
||||
/// rsx!{ div{ "hello {cx.name}" } })
|
||||
/// }
|
||||
///
|
||||
/// let dom = VirtualDom::new(Example);
|
||||
/// ```
|
||||
///
|
||||
/// Note: the VirtualDom is not progressed on creation. You must either "run_with_deadline" or use "rebuild" to progress it.
|
||||
///
|
||||
/// ```rust, ignore
|
||||
/// let mut dom = VirtualDom::new_from_root(VComponent::new(Example, SomeProps { name: "jane" }, "Example"));
|
||||
/// let mutations = dom.rebuild();
|
||||
/// ```
|
||||
pub fn new_with_component(root: VComponent) -> Self {
|
||||
let (tx, rx) = futures_channel::mpsc::unbounded();
|
||||
|
||||
let mut dom = Self {
|
||||
@@ -285,10 +324,7 @@ impl VirtualDom {
|
||||
suspended_scopes: Default::default(),
|
||||
};
|
||||
|
||||
let root = dom.new_scope(
|
||||
new_any_props(Rc::new(root).as_component(), |_, _| true, root_props, "app"),
|
||||
"app",
|
||||
);
|
||||
let root = dom.new_scope(root.props, "app");
|
||||
|
||||
// Unlike react, we provide a default error boundary that just renders the error as a string
|
||||
root.context()
|
||||
|
||||
Reference in new issue
Block a user