diff --git a/packages/desktop/src/launch.rs b/packages/desktop/src/launch.rs index 334f32f95..cd32a7a94 100644 --- a/packages/desktop/src/launch.rs +++ b/packages/desktop/src/launch.rs @@ -66,7 +66,7 @@ pub fn launch_virtual_dom(virtual_dom: VirtualDom, desktop_config: Config) { /// Launches the WebView and runs the event loop, with configuration and root props. pub fn launch( root: fn() -> Element, - contexts: Vec Box + Send + Sync>>, + contexts: Vec Box>>, platform_config: Config, ) { let mut virtual_dom = VirtualDom::new(root); diff --git a/packages/dioxus/src/launch.rs b/packages/dioxus/src/launch.rs index 61fe7a4af..b43eb9bc4 100644 --- a/packages/dioxus/src/launch.rs +++ b/packages/dioxus/src/launch.rs @@ -8,22 +8,27 @@ use dioxus_core::AnyProps; use dioxus_core::VProps; /// A builder for a fullstack app. -pub struct LaunchBuilder { - launch_fn: LaunchFn, - contexts: ContextList, +pub struct LaunchBuilder { + launch_fn: LaunchFn, + contexts: Vec>, platform_config: Option, } -pub type LaunchFn = fn(fn() -> Element, ContextList, Cfg); +pub type LaunchFn = fn(fn() -> Element, Vec>, Cfg); -// #[cfg(feature = "fullstack")] -type ContextList = Vec Box + Send + Sync>>; -// #[cfg(not(feature = "fullstack"))] -// type ContextList = Vec Box>>; +#[cfg(feature = "fullstack")] +type ValidContext = SendContext; + +#[cfg(not(feature = "fullstack"))] +type ValidContext = UnsendContext; + +type SendContext = dyn Fn() -> Box + Send + Sync + 'static; + +type UnsendContext = dyn Fn() -> Box + 'static; impl LaunchBuilder { /// 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() -> LaunchBuilder { + pub fn new() -> LaunchBuilder { LaunchBuilder { launch_fn: current_platform::launch, contexts: Vec::new(), @@ -33,7 +38,7 @@ impl LaunchBuilder { /// Launch your web application. #[cfg(feature = "web")] - pub fn web() -> LaunchBuilder { + pub fn web() -> LaunchBuilder { LaunchBuilder { launch_fn: dioxus_web::launch::launch, contexts: Vec::new(), @@ -43,7 +48,7 @@ impl LaunchBuilder { /// Launch your desktop application. #[cfg(feature = "desktop")] - pub fn desktop() -> LaunchBuilder { + pub fn desktop() -> LaunchBuilder { LaunchBuilder { launch_fn: dioxus_desktop::launch::launch, contexts: Vec::new(), @@ -53,7 +58,7 @@ impl LaunchBuilder { /// Launch your fullstack application. #[cfg(feature = "fullstack")] - pub fn fullstack() -> LaunchBuilder { + pub fn fullstack() -> LaunchBuilder { LaunchBuilder { launch_fn: dioxus_fullstack::launch::launch, contexts: Vec::new(), @@ -74,47 +79,35 @@ impl LaunchBuilder { /// Provide a custom launch function for your application. /// /// Useful for third party renderers to tap into the launch builder API without having to reimplement it. - pub fn custom(launch_fn: LaunchFn) -> LaunchBuilder { + pub fn custom(launch_fn: LaunchFn) -> LaunchBuilder { LaunchBuilder { launch_fn, - contexts: Vec::new(), + contexts: vec![], platform_config: None, } } } // Fullstack platform builder -impl LaunchBuilder { - #[cfg(feature = "fullstack")] +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 + Send + Sync + 'static, - ) -> Self { - self.contexts - .push(Box::new(state) as Box Box + Send + Sync>); - self - } - #[cfg(not(feature = "fullstack"))] - /// 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 + 'static) -> Self { - self.contexts - .push(Box::new(state) as Box Box>); + #[cfg(feature = "fullstack")] state: impl Fn() -> Box + Send + Sync + 'static, + #[cfg(not(feature = "fullstack"))] state: impl Fn() -> Box + 'static, + ) -> Self { + self.contexts.push(Box::new(state) as Box); self } #[cfg(feature = "fullstack")] /// Inject state into the root component's context. - pub fn with_context(mut self, state: impl Any + Clone + Send + Sync + 'static) -> Self { - self.contexts - .push(Box::new(move || Box::new(state.clone()))); - self - } - - #[cfg(not(feature = "fullstack"))] - /// Inject state into the root component's context. - pub fn with_context(mut self, state: impl Any + Clone + 'static) -> Self { + pub fn with_context( + mut self, + #[cfg(feature = "fullstack")] state: impl Any + Clone + Send + Sync + 'static, + #[cfg(not(feature = "fullstack"))] state: impl Any + Clone + 'static, + ) -> Self { self.contexts .push(Box::new(move || Box::new(state.clone()))); self diff --git a/packages/fullstack/examples/axum-router/src/main.rs b/packages/fullstack/examples/axum-router/src/main.rs index 8e8df2c1f..72a9f5ff8 100644 --- a/packages/fullstack/examples/axum-router/src/main.rs +++ b/packages/fullstack/examples/axum-router/src/main.rs @@ -21,8 +21,7 @@ fn main() { #[cfg(not(feature = "ssr"))] config.launch(|| { rsx! { - Router:: { - } + Router:: {} } }); } @@ -31,6 +30,7 @@ fn main() { enum Route { #[route("/")] Home {}, + #[route("/blog/:id")] Blog { id: i32 }, } diff --git a/packages/web/src/launch.rs b/packages/web/src/launch.rs index b647f715f..760bcf87b 100644 --- a/packages/web/src/launch.rs +++ b/packages/web/src/launch.rs @@ -9,7 +9,7 @@ use std::any::Any; /// For a builder API, see `LaunchBuilder` defined in the `dioxus` crate. pub fn launch( root: fn() -> Element, - contexts: Vec Box + Send + Sync>>, + contexts: Vec Box>>, platform_config: Config, ) { let mut vdom = VirtualDom::new(root);