Assert that launch never returns for better compiler errors (#2517)

* assert that launch never returns for better compiler errors

* fix static generation launch function

* fix web renderer
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-06-18 16:04:36 -07:00
1 parent 5e3bb4f549
commit f4a62fad6b
5 files changed
+122 -67

No files matched your search

+63 -34
View File
@@ -6,6 +6,39 @@ use dioxus_lib::prelude::{Element, VirtualDom};
pub use crate::Config;
fn virtual_dom_factory(
root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
) -> impl Fn() -> VirtualDom + 'static {
move || {
let mut vdom = VirtualDom::new(root);
for context in &contexts {
vdom.insert_any_root_context(context());
}
vdom
}
}
#[cfg(feature = "server")]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
platform_config: Config,
) -> ! {
let factory = virtual_dom_factory(root, contexts);
#[cfg(all(feature = "server", not(target_arch = "wasm32")))]
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
platform_config.launch_server(factory).await;
});
unreachable!("Launching a fullstack app should never return")
}
#[cfg(all(not(feature = "server"), feature = "web"))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
@@ -13,40 +46,36 @@ pub fn launch(
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
platform_config: Config,
) {
let virtual_dom_factory = move || {
let mut vdom = VirtualDom::new(root);
for context in &contexts {
vdom.insert_any_root_context(context());
}
vdom
};
let factory = virtual_dom_factory(root, contexts);
let cfg = platform_config.web_cfg.hydrate(true);
dioxus_web::launch::launch_virtual_dom(factory(), cfg)
}
#[cfg(all(feature = "server", not(target_arch = "wasm32")))]
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
platform_config.launch_server(virtual_dom_factory).await;
});
#[cfg(all(not(any(feature = "server", feature = "web")), feature = "desktop"))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
platform_config: Config,
) -> ! {
let factory = virtual_dom_factory(root, contexts);
let cfg = platform_config.desktop_cfg;
dioxus_desktop::launch::launch_virtual_dom(factory(), cfg)
}
#[cfg(not(feature = "server"))]
{
#[cfg(feature = "web")]
{
// TODO: this should pull the props from the document
let cfg = platform_config.web_cfg.hydrate(true);
dioxus_web::launch::launch_virtual_dom(virtual_dom_factory(), cfg);
}
#[cfg(feature = "desktop")]
{
let cfg = platform_config.desktop_cfg;
dioxus_desktop::launch::launch_virtual_dom(virtual_dom_factory(), cfg)
}
#[cfg(feature = "mobile")]
{
let cfg = platform_config.mobile_cfg;
dioxus_mobile::launch::launch_virtual_dom(virtual_dom_factory(), cfg)
}
}
#[cfg(all(
not(any(feature = "server", feature = "web", feature = "desktop")),
feature = "mobile"
))]
/// Launch a fullstack app with the given root component, contexts, and config.
#[allow(unused)]
pub fn launch(
root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
platform_config: Config,
) {
let factory = virtual_dom_factory(root, contexts);
let cfg = platform_config.mobile_cfg;
dioxus_mobile::launch::launch_virtual_dom(factory(), cfg)
}