Rename to component function

This commit is contained in:
Jonathan Kelley 2024-01-16 15:09:44 -08:00
parent c94af9538b
commit 3fb7c359c2
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
6 changed files with 30 additions and 21 deletions

View file

@ -75,7 +75,7 @@ pub(crate) mod innerlude {
pub use crate::innerlude::{
fc_to_builder, generation, schedule_update, schedule_update_any, use_hook, vdom_is_rendering,
AnyValue, Attribute, AttributeValue, BoxedContext, CapturedError, Component,
AnyValue, Attribute, AttributeValue, BoxedContext, CapturedError, Component, ComponentFunction,
CrossPlatformConfig, DynamicNode, Element, ElementId, Event, Fragment, HasAttributes,
IntoDynNode, Mutation, Mutations, NoOpMutations, PlatformBuilder, Properties, RenderReturn,
ScopeId, ScopeState, Task, Template, TemplateAttribute, TemplateNode, VComponent, VNode,
@ -91,7 +91,7 @@ pub mod prelude {
has_context, needs_update, parent_scope, provide_context, provide_root_context,
remove_future, schedule_update, schedule_update_any, spawn, spawn_forever, suspend,
try_consume_context, use_error_boundary, use_hook, AnyValue, Attribute, Component,
ComponentFn, Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes,
ComponentFunction, Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes,
IntoAttributeValue, IntoDynNode, Properties, Runtime, RuntimeGuard, ScopeId, ScopeState,
Task, Template, TemplateAttribute, TemplateNode, Throw, VNode, VNodeInner, VirtualDom,
};

View file

@ -5,7 +5,7 @@ use crate::{
use crate::{arena::ElementId, Element, Event};
use crate::{
innerlude::{ElementRef, EventHandler, MountId},
properties::ComponentFn,
properties::ComponentFunction,
};
use crate::{Properties, VirtualDom};
use core::panic;
@ -533,7 +533,11 @@ impl VComponent {
/// fn(Props) -> Element;
/// async fn(Scope<Props<'_>>) -> Element;
/// ```
pub fn new<P, M>(component: impl ComponentFn<P, M>, props: P, fn_name: &'static str) -> Self
pub fn new<P, M>(
component: impl ComponentFunction<P, M>,
props: P,
fn_name: &'static str,
) -> Self
where
// The properties must be valid until the next bump frame
P: Properties + 'static,

View file

@ -1,6 +1,6 @@
use std::any::Any;
use crate::{properties::ComponentFn, Component, VirtualDom};
use crate::{properties::ComponentFunction, Component, VirtualDom};
/// A boxed object that can be injected into a component's context.
pub struct BoxedContext(Box<dyn ClonableAny>);
@ -52,12 +52,12 @@ pub struct CrossPlatformConfig<Props: Clone + 'static> {
impl<Props: Clone + 'static> CrossPlatformConfig<Props> {
/// Create a new cross-platform config.
pub fn new<M>(
component: impl ComponentFn<Props, M>,
component: impl ComponentFunction<Props, M>,
props: Props,
root_contexts: Vec<BoxedContext>,
) -> Self {
Self {
component: ComponentFn::as_component(std::rc::Rc::new(component)),
component: ComponentFunction::as_component(std::rc::Rc::new(component)),
props,
root_contexts,
}

View file

@ -54,7 +54,7 @@ impl EmptyBuilder {
/// This utility function launches the builder method so rsx! and html! macros can use the typed-builder pattern
/// to initialize a component's props.
pub fn fc_to_builder<P, M>(_: impl ComponentFn<P, M>) -> <P as Properties>::Builder
pub fn fc_to_builder<P, M>(_: impl ComponentFunction<P, M>) -> <P as Properties>::Builder
where
P: Properties,
{
@ -62,20 +62,23 @@ where
}
/// Any component that implements the `ComponentFn` trait can be used as a component.
pub trait ComponentFn<Props, Marker> {
pub trait ComponentFunction<Props, Marker> {
type Props;
/// Convert the component to a function that takes props and returns an element.
fn as_component(self: Rc<Self>) -> Component<Props>;
}
/// Accept pre-formed component render functions as components
impl<P> ComponentFn<P, ()> for Component<P> {
impl<P> ComponentFunction<P, ()> for Component<P> {
type Props = P;
fn as_component(self: Rc<Self>) -> Component<P> {
self.as_ref().clone()
}
}
/// Accept any callbacks that take props
impl<F: Fn(P) -> Element + 'static, P> ComponentFn<P, ()> for F {
impl<F: Fn(P) -> Element + 'static, P> ComponentFunction<P, ()> for F {
type Props = P;
fn as_component(self: Rc<Self>) -> Component<P> {
self
}
@ -83,7 +86,8 @@ impl<F: Fn(P) -> Element + 'static, P> ComponentFn<P, ()> for F {
/// Accept any callbacks that take no props
pub struct EmptyMarker;
impl<F: Fn() -> Element + 'static> ComponentFn<(), EmptyMarker> for F {
impl<F: Fn() -> Element + 'static> ComponentFunction<(), EmptyMarker> for F {
type Props = ();
fn as_component(self: Rc<Self>) -> Rc<dyn Fn(()) -> Element> {
Rc::new(move |_| self())
}
@ -98,7 +102,8 @@ fn it_works_maybe() {
todo!()
}
let callable: Rc<dyn ComponentFn<(), ()>> = Rc::new(test) as Rc<dyn ComponentFn<_, _>>;
let callable2: Rc<dyn ComponentFn<(), EmptyMarker>> =
Rc::new(test2) as Rc<dyn ComponentFn<_, _>>;
let callable: Rc<dyn ComponentFunction<(), ()>> =
Rc::new(test) as Rc<dyn ComponentFunction<_, _>>;
let callable2: Rc<dyn ComponentFunction<(), EmptyMarker>> =
Rc::new(test2) as Rc<dyn ComponentFunction<_, _>>;
}

View file

@ -11,7 +11,7 @@ use crate::{
},
nodes::RenderReturn,
nodes::{Template, TemplateId},
properties::ComponentFn,
properties::ComponentFunction,
runtime::{Runtime, RuntimeGuard},
scopes::ScopeId,
AttributeValue, BoxedContext, Element, Event, Mutations,
@ -269,7 +269,7 @@ impl VirtualDom {
/// let mutations = dom.rebuild();
/// ```
pub fn new_with_props<P: Clone + 'static, M>(
root: impl ComponentFn<P, M>,
root: impl ComponentFunction<P, M>,
root_props: P,
) -> Self {
let (tx, rx) = futures_channel::mpsc::unbounded();

View file

@ -16,7 +16,7 @@ pub struct LaunchBuilder<Props: Clone + 'static, Platform: PlatformBuilder<Props
// Default platform builder
impl<Props: Clone + 'static> LaunchBuilder<Props> {
/// Create a new builder for your application. This will create a launch configuration for the current platform based on the features enabled on the `dioxus` crate.
pub fn new<M>(component: impl ComponentFn<Props, M>) -> Self
pub fn new<M>(component: impl ComponentFunction<Props, M>) -> Self
where
Props: Default,
{
@ -97,7 +97,7 @@ type CurrentPlatform = dioxus_web::WebPlatform;
type CurrentPlatform = ();
/// Launch your application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch<Props, Marker>(component: impl ComponentFn<Props, Marker>)
pub fn launch<Props, Marker>(component: impl ComponentFunction<Props, Marker>)
where
Props: Default + Clone + 'static,
{
@ -106,7 +106,7 @@ where
#[cfg(feature = "web")]
/// Launch your web application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_web<Props, Marker>(component: impl ComponentFn<Props, Marker>)
pub fn launch_web<Props, Marker>(component: impl ComponentFunction<Props, Marker>)
where
Props: Default + Clone + 'static,
{
@ -115,7 +115,7 @@ where
#[cfg(feature = "desktop")]
/// Launch your desktop application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_desktop<Props, Marker>(component: impl ComponentFn<Props, Marker>)
pub fn launch_desktop<Props, Marker>(component: impl ComponentFunction<Props, Marker>)
where
Props: Default + Clone + 'static,
{