Simplify the launch builder (#2967)

* improve documentation for the fullstack server context

* Add a section about axum integration to the crate root docs

* make serve_dioxus_application accept the cfg builder directly

* remove unused server_fn module

* improve fullstack config docs

* improve documentation for the server function macro

* fix axum router extension link

* Fix doc tests

* Fix launch builder

* Simplify the launch builder

* don't re-export launch in the prelude

* refactor fullstack launch

* Fix fullstack launch builder

* Update static generation with the new builder api

* fix some formatting/overly broad launch replacements

* fix custom menu example

* fix fullstack/static generation examples

* Fix static generation launch

* A few small formatting fixes

* Fix a few doc tests

* implement LaunchConfig for serve configs

* fix fullstack launch with separate web and server launch methods

* fix check with all features

* dont expose inner core module

* clippy and check

* fix readme

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Evan AlmloffandJonathan Kelley authored and GitHub committed 2024-10-10 16:00:58 -07:00
1 parent 2d7bef44e8
commit 20d146d9bd
126 files changed
+618 -989

No files matched your search

+2 -2
View File
@@ -42,7 +42,7 @@ To launch an app, we use the `launch` method and use features in `Cargo.toml` to
use dioxus::prelude::*;
fn main() {
launch(App);
dioxus::launch(App);
}
// The #[component] attribute streamlines component creation.
@@ -184,7 +184,7 @@ Using components, rsx, and hooks, we can build a simple app.
use dioxus::prelude::*;
fn main() {
launch(App);
dioxus::launch(App);
}
#[component]
+127 -251
View File
@@ -2,20 +2,22 @@
#![allow(clippy::new_without_default)]
#![allow(unused)]
use dioxus_config_macro::*;
use dioxus_core::LaunchConfig;
use std::any::Any;
use crate::prelude::*;
/// A builder for a fullstack app.
#[must_use]
pub struct LaunchBuilder<Cfg: 'static = (), ContextFn: ?Sized = ValidContext> {
launch_fn: LaunchFn<Cfg, ContextFn>,
contexts: Vec<Box<ContextFn>>,
platform_config: Option<Cfg>,
pub struct LaunchBuilder {
launch_fn: LaunchFn,
contexts: Vec<ContextFn>,
configs: Vec<Box<dyn Any>>,
}
pub type LaunchFn<Cfg, Context> = fn(fn() -> Element, Vec<Box<Context>>, Cfg);
pub type LaunchFn = fn(fn() -> Element, Vec<ContextFn>, Vec<Box<dyn Any>>);
/// A context function is a Send and Sync closure that returns a boxed trait object
pub type ContextFn = Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>;
#[cfg(any(
feature = "fullstack",
@@ -54,69 +56,74 @@ impl LaunchBuilder {
note = "No renderer is enabled. You must enable a renderer feature on the dioxus crate before calling the launch function.\nAdd `web`, `desktop`, `mobile`, `fullstack`, or `static-generation` to the `features` of dioxus field in your Cargo.toml.\n# Example\n```toml\n# ...\n[dependencies]\ndioxus = { version = \"0.5.0\", features = [\"web\"] }\n# ...\n```"
)
)]
pub fn new() -> LaunchBuilder<current_platform::Config, ValidContext> {
pub fn new() -> LaunchBuilder {
LaunchBuilder {
// We can't use the `current_platform::launch` function directly because it may return ! or ()
launch_fn: |root, contexts, cfg| current_platform::launch(root, contexts, cfg),
contexts: Vec::new(),
platform_config: None,
configs: Vec::new(),
}
}
/// Launch your web application.
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
pub fn web() -> LaunchBuilder<dioxus_web::Config, UnsendContext> {
pub fn web() -> LaunchBuilder {
LaunchBuilder {
launch_fn: dioxus_web::launch::launch,
launch_fn: web_launch,
contexts: Vec::new(),
platform_config: None,
configs: Vec::new(),
}
}
/// Launch your desktop application.
#[cfg(feature = "desktop")]
#[cfg_attr(docsrs, doc(cfg(feature = "desktop")))]
pub fn desktop() -> LaunchBuilder<dioxus_desktop::Config, UnsendContext> {
pub fn desktop() -> LaunchBuilder {
LaunchBuilder {
launch_fn: |root, contexts, cfg| dioxus_desktop::launch::launch(root, contexts, cfg),
contexts: Vec::new(),
platform_config: None,
configs: Vec::new(),
}
}
/// Launch your fullstack application.
#[cfg(feature = "fullstack")]
#[cfg_attr(docsrs, doc(cfg(feature = "fullstack")))]
pub fn fullstack() -> LaunchBuilder<dioxus_fullstack::Config, SendContext> {
/// Launch your fullstack axum server.
#[cfg(all(feature = "fullstack", feature = "server"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "fullstack", feature = "server"))))]
pub fn server() -> LaunchBuilder {
LaunchBuilder {
launch_fn: |root, contexts, cfg| dioxus_fullstack::launch::launch(root, contexts, cfg),
launch_fn: |root, contexts, cfg| {
dioxus_fullstack::server::launch::launch(root, contexts, cfg)
},
contexts: Vec::new(),
platform_config: None,
configs: Vec::new(),
}
}
/// Launch your static site generation application.
#[cfg(feature = "static-generation")]
#[cfg_attr(docsrs, doc(cfg(feature = "static-generation")))]
pub fn static_generation() -> LaunchBuilder<dioxus_static_site_generation::Config, SendContext>
{
#[cfg(all(feature = "static-generation", feature = "server"))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "static-generation", feature = "server")))
)]
pub fn static_generation() -> LaunchBuilder {
LaunchBuilder {
launch_fn: |root, contexts, cfg| {
dioxus_static_site_generation::launch::launch(root, contexts, cfg)
},
contexts: Vec::new(),
platform_config: None,
configs: Vec::new(),
}
}
/// Launch your fullstack application.
#[cfg(feature = "mobile")]
#[cfg_attr(docsrs, doc(cfg(feature = "mobile")))]
pub fn mobile() -> LaunchBuilder<dioxus_mobile::Config, UnsendContext> {
pub fn mobile() -> LaunchBuilder {
LaunchBuilder {
launch_fn: |root, contexts, cfg| dioxus_mobile::launch::launch(root, contexts, cfg),
contexts: Vec::new(),
platform_config: None,
configs: Vec::new(),
}
}
@@ -132,7 +139,7 @@ impl LaunchBuilder {
/// #[derive(Default)]
/// struct Config;
///
/// fn my_custom_launcher(root: fn() -> Element, contexts: Vec<Box<dyn Any>>, cfg: Config) {
/// fn my_custom_launcher(root: fn() -> Element, contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>, cfg: Vec<Box<dyn Any>>) {
/// println!("launching with root: {:?}", root());
/// loop {
/// println!("running...");
@@ -145,42 +152,24 @@ impl LaunchBuilder {
/// }
/// }
///
/// LaunchBuilder::custom(my_custom_launcher).launch(app);
/// dioxus::LaunchBuilder::custom(my_custom_launcher).launch(app);
/// ```
pub fn custom<Config, Context: ?Sized>(
launch_fn: LaunchFn<Config, Context>,
) -> LaunchBuilder<Config, Context> {
pub fn custom(launch_fn: LaunchFn) -> LaunchBuilder {
LaunchBuilder {
launch_fn,
contexts: vec![],
platform_config: None,
configs: Vec::new(),
}
}
}
// Fullstack platform builder
impl<Cfg> LaunchBuilder<Cfg, UnsendContext> {
/// Inject state into the root component's context that is created on the thread that the app is launched on.
pub fn with_context_provider(mut self, state: impl Fn() -> Box<dyn Any> + 'static) -> Self {
self.contexts.push(Box::new(state) as Box<UnsendContext>);
self
}
/// Inject state into the root component's context.
pub fn with_context(mut self, state: impl Any + Clone + 'static) -> Self {
self.contexts
.push(Box::new(move || Box::new(state.clone())));
self
}
}
impl<Cfg> LaunchBuilder<Cfg, SendContext> {
impl LaunchBuilder {
/// Inject state into the root component's context that is created on the thread that the app is launched on.
pub fn with_context_provider(
mut self,
state: impl Fn() -> Box<dyn Any + Send + Sync> + Send + Sync + 'static,
state: impl Fn() -> Box<dyn Any> + Send + Sync + 'static,
) -> Self {
self.contexts.push(Box::new(state) as Box<SendContext>);
self.contexts.push(Box::new(state));
self
}
@@ -192,38 +181,10 @@ impl<Cfg> LaunchBuilder<Cfg, SendContext> {
}
}
/// A trait for converting a type into a platform-specific config:
/// - A unit value will be converted into `None`
/// - Any config will be converted into `Some(config)`
/// - If the config is for another platform, it will be converted into `None`
pub trait TryIntoConfig<Config = Self> {
fn into_config(self, config: &mut Option<Config>);
}
// A config can always be converted into itself
impl<Cfg> TryIntoConfig<Cfg> for Cfg {
fn into_config(self, config: &mut Option<Cfg>) {
*config = Some(self);
}
}
// The unit type can be converted into the current platform config.
// This makes it possible to use the `desktop!`, `web!`, etc macros with the launch API.
#[cfg(any(
feature = "liveview",
feature = "desktop",
feature = "mobile",
feature = "web",
feature = "fullstack"
))]
impl TryIntoConfig<current_platform::Config> for () {
fn into_config(self, config: &mut Option<current_platform::Config>) {}
}
impl<Cfg: Default + 'static, ContextFn: ?Sized> LaunchBuilder<Cfg, ContextFn> {
impl LaunchBuilder {
/// Provide a platform-specific config to the builder.
pub fn with_cfg(mut self, config: impl TryIntoConfig<Cfg>) -> Self {
config.into_config(&mut self.platform_config);
pub fn with_cfg(mut self, config: impl LaunchConfig) -> Self {
self.configs.push(Box::new(config));
self
}
@@ -231,7 +192,7 @@ impl<Cfg: Default + 'static, ContextFn: ?Sized> LaunchBuilder<Cfg, ContextFn> {
#[cfg(any(feature = "static-generation", feature = "web"))]
/// Launch your application.
pub fn launch(self, app: fn() -> Element) {
let cfg = self.platform_config.unwrap_or_default();
let cfg = self.configs;
(self.launch_fn)(app, self.contexts, cfg)
}
@@ -239,7 +200,7 @@ impl<Cfg: Default + 'static, ContextFn: ?Sized> LaunchBuilder<Cfg, ContextFn> {
#[cfg(not(any(feature = "static-generation", feature = "web")))]
/// Launch your application.
pub fn launch(self, app: fn() -> Element) -> ! {
let cfg = self.platform_config.unwrap_or_default();
let cfg = self.configs;
(self.launch_fn)(app, self.contexts, cfg);
unreachable!("Launching an application will never exit")
@@ -256,147 +217,67 @@ impl<Cfg: Default + 'static, ContextFn: ?Sized> LaunchBuilder<Cfg, ContextFn> {
/// - `web`
/// - `liveview`
mod current_platform {
macro_rules! if_else_cfg {
(if $attr:meta { $($then:item)* } else { $($else:item)* }) => {
$(
#[cfg($attr)]
$then
)*
$(
#[cfg(not($attr))]
$else
)*
};
}
use crate::prelude::TryIntoConfig;
#[cfg(all(feature = "fullstack", feature = "server"))]
pub use dioxus_fullstack::server::launch::*;
#[cfg(feature = "fullstack")]
pub use dioxus_fullstack::launch::*;
#[cfg(all(
feature = "desktop",
not(all(feature = "fullstack", feature = "server"))
))]
pub use dioxus_desktop::launch::*;
#[cfg(all(feature = "fullstack", feature = "axum"))]
impl TryIntoConfig<crate::launch::current_platform::Config>
for ::dioxus_fullstack::prelude::ServeConfigBuilder
{
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {
match config {
Some(config) => config.set_server_config(self),
None => {
*config = Some(
crate::launch::current_platform::Config::new().with_server_config(self),
)
}
}
}
#[cfg(all(
feature = "mobile",
not(feature = "desktop"),
not(all(feature = "fullstack", feature = "server"))
))]
pub use dioxus_mobile::launch::*;
#[cfg(all(
all(feature = "static-generation", feature = "server"),
not(all(feature = "fullstack", feature = "server")),
not(feature = "desktop"),
not(feature = "mobile")
))]
pub use dioxus_static_site_generation::launch::*;
#[cfg(all(
feature = "web",
not(all(feature = "fullstack", feature = "server")),
not(all(feature = "static-generation", feature = "server")),
not(feature = "desktop"),
not(feature = "mobile"),
))]
pub fn launch(
root: fn() -> dioxus_core::Element,
contexts: Vec<super::ContextFn>,
platform_config: Vec<Box<dyn std::any::Any>>,
) {
super::web_launch(root, contexts, platform_config);
}
#[cfg(any(feature = "desktop", feature = "mobile"))]
if_else_cfg! {
if not(feature = "fullstack") {
#[cfg(feature = "desktop")]
pub use dioxus_desktop::launch::*;
#[cfg(not(feature = "desktop"))]
pub use dioxus_mobile::launch::*;
} else {
if_else_cfg! {
if feature = "desktop" {
impl TryIntoConfig<crate::launch::current_platform::Config> for ::dioxus_desktop::Config {
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {
match config {
Some(config) => config.set_desktop_config(self),
None => *config = Some(crate::launch::current_platform::Config::new().with_desktop_config(self)),
}
}
}
} else {
impl TryIntoConfig<crate::launch::current_platform::Config> for ::dioxus_mobile::Config {
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {
match config {
Some(config) => config.set_mobile_cfg(self),
None => *config = Some(crate::launch::current_platform::Config::new().with_mobile_cfg(self)),
}
}
}
}
}
}
}
#[cfg(feature = "static-generation")]
if_else_cfg! {
if all(not(feature = "fullstack"), not(feature = "desktop"), not(feature = "mobile")) {
pub use dioxus_static_site_generation::launch::*;
} else {
impl TryIntoConfig<crate::launch::current_platform::Config> for ::dioxus_static_site_generation::Config {
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {}
}
}
}
#[cfg(feature = "web")]
if_else_cfg! {
if not(any(feature = "desktop", feature = "mobile", feature = "fullstack", feature = "static-generation")) {
pub use dioxus_web::launch::*;
} else {
if_else_cfg! {
if feature = "fullstack" {
impl TryIntoConfig<crate::launch::current_platform::Config> for ::dioxus_web::Config {
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {
match config {
Some(config) => config.set_web_config(self),
None => *config = Some(crate::launch::current_platform::Config::new().with_web_config(self)),
}
}
}
} else {
impl TryIntoConfig<crate::launch::current_platform::Config> for ::dioxus_web::Config {
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {}
}
}
}
}
}
#[cfg(feature = "liveview")]
if_else_cfg! {
if
not(any(
feature = "web",
feature = "desktop",
feature = "mobile",
feature = "fullstack",
feature = "static-generation"
))
{
pub use dioxus_liveview::launch::*;
} else {
impl<R: ::dioxus_liveview::LiveviewRouter> TryIntoConfig<crate::launch::current_platform::Config> for ::dioxus_liveview::Config<R> {
fn into_config(self, config: &mut Option<crate::launch::current_platform::Config>) {}
}
}
}
#[cfg(all(
feature = "liveview",
not(all(feature = "fullstack", feature = "server")),
not(all(feature = "static-generation", feature = "server")),
not(feature = "desktop"),
not(feature = "mobile"),
not(feature = "web"),
))]
pub use dioxus_liveview::launch::*;
#[cfg(not(any(
feature = "liveview",
all(feature = "fullstack", feature = "server"),
all(feature = "static-generation", feature = "server"),
feature = "desktop",
feature = "mobile",
feature = "web",
feature = "fullstack",
feature = "static-generation"
)))]
pub type Config = ();
#[cfg(not(any(
feature = "liveview",
feature = "desktop",
feature = "mobile",
feature = "web",
feature = "fullstack",
feature = "static-generation"
)))]
pub fn launch(
root: fn() -> dioxus_core::Element,
contexts: Vec<Box<super::ValidContext>>,
platform_config: (),
contexts: Vec<super::ContextFn>,
platform_config: Vec<Box<dyn std::any::Any>>,
) -> ! {
#[cfg(feature = "third-party-renderer")]
panic!("No first party renderer feature enabled. It looks like you are trying to use a third party renderer. You will need to use the launch function from the third party renderer crate.");
@@ -409,22 +290,6 @@ mod current_platform {
macro_rules! impl_launch {
($($return_type:tt),*) => {
/// Launch your application without any additional configuration. See [`LaunchBuilder`] for more options.
// If you aren't using a third party renderer and this is not a docs.rs build, generate a warning about no renderer being enabled
#[cfg_attr(
all(not(any(
docsrs,
feature = "third-party-renderer",
feature = "liveview",
feature = "desktop",
feature = "mobile",
feature = "web",
feature = "fullstack",
feature = "static-generation"
))),
deprecated(
note = "No renderer is enabled. You must enable a renderer feature on the dioxus crate before calling the launch function.\nAdd `web`, `desktop`, `mobile`, `fullstack`, or `static-generation` to the `features` of dioxus field in your Cargo.toml.\n# Example\n```toml\n# ...\n[dependencies]\ndioxus = { version = \"0.5.0\", features = [\"web\"] }\n# ...\n```"
)
)]
pub fn launch(app: fn() -> Element) -> $($return_type)* {
#[allow(deprecated)]
LaunchBuilder::new().launch(app)
@@ -439,29 +304,40 @@ impl_launch!(());
impl_launch!(!);
#[cfg(feature = "web")]
#[cfg_attr(docsrs, doc(cfg(feature = "web")))]
/// Launch your web application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_web(app: fn() -> Element) {
LaunchBuilder::web().launch(app)
}
fn web_launch(
root: fn() -> dioxus_core::Element,
contexts: Vec<super::ContextFn>,
platform_config: Vec<Box<dyn std::any::Any>>,
) {
// If the server feature is enabled, launch the client with hydration enabled
#[cfg(any(feature = "static-generation", feature = "fullstack"))]
{
let platform_config = platform_config
.into_iter()
.find_map(|cfg| cfg.downcast::<dioxus_web::Config>().ok())
.unwrap_or_default()
.hydrate(true);
#[cfg(feature = "desktop")]
#[cfg_attr(docsrs, doc(cfg(feature = "desktop")))]
/// Launch your desktop application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_desktop(app: fn() -> Element) {
LaunchBuilder::desktop().launch(app)
}
let factory = move || {
let mut vdom = dioxus_core::VirtualDom::new(root);
for context in contexts {
vdom.insert_any_root_context(context());
}
#[cfg(feature = "document")]
{
#[cfg(feature = "fullstack")]
use dioxus_fullstack::document;
#[cfg(all(feature = "static-generation", not(feature = "fullstack")))]
use dioxus_static_site_generation::document;
let document = std::rc::Rc::new(document::web::FullstackWebDocument)
as std::rc::Rc<dyn crate::prelude::Document>;
vdom.provide_root_context(document);
}
vdom
};
#[cfg(feature = "fullstack")]
#[cfg_attr(docsrs, doc(cfg(feature = "fullstack")))]
/// Launch your fullstack application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_fullstack(app: fn() -> Element) {
LaunchBuilder::fullstack().launch(app)
}
#[cfg(feature = "mobile")]
#[cfg_attr(docsrs, doc(cfg(feature = "mobile")))]
/// Launch your mobile application without any additional configuration. See [`LaunchBuilder`] for more options.
pub fn launch_mobile(app: fn() -> Element) {
LaunchBuilder::mobile().launch(app)
dioxus_web::launch::launch_virtual_dom(factory(), platform_config)
}
#[cfg(not(any(feature = "static-generation", feature = "fullstack")))]
dioxus_web::launch::launch(root, contexts, platform_config);
}
+1 -5
View File
@@ -34,8 +34,7 @@ mod launch;
#[cfg(feature = "launch")]
#[cfg_attr(docsrs, doc(cfg(feature = "launch")))]
#[allow(deprecated)]
pub use launch::launch;
pub use crate::launch::*;
#[cfg(feature = "hooks")]
#[cfg_attr(docsrs, doc(cfg(feature = "hooks")))]
@@ -60,9 +59,6 @@ pub use dioxus_html as html;
pub use dioxus_core_macro as core_macro;
pub mod prelude {
#[cfg(feature = "launch")]
#[cfg_attr(docsrs, doc(cfg(feature = "launch")))]
pub use crate::launch::*;
#[cfg(feature = "hooks")]
#[cfg_attr(docsrs, doc(cfg(feature = "hooks")))]