Fix context trait params
This commit is contained in:
4 files changed
+33
-40
No files matched your search
@@ -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.
|
/// Launches the WebView and runs the event loop, with configuration and root props.
|
||||||
pub fn launch(
|
pub fn launch(
|
||||||
root: fn() -> Element,
|
root: fn() -> Element,
|
||||||
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
|
contexts: Vec<Box<dyn Fn() -> Box<dyn Any>>>,
|
||||||
platform_config: Config,
|
platform_config: Config,
|
||||||
) {
|
) {
|
||||||
let mut virtual_dom = VirtualDom::new(root);
|
let mut virtual_dom = VirtualDom::new(root);
|
||||||
|
|||||||
@@ -8,22 +8,27 @@ use dioxus_core::AnyProps;
|
|||||||
use dioxus_core::VProps;
|
use dioxus_core::VProps;
|
||||||
|
|
||||||
/// A builder for a fullstack app.
|
/// A builder for a fullstack app.
|
||||||
pub struct LaunchBuilder<Cfg = ()> {
|
pub struct LaunchBuilder<Cfg = (), ContextFn: ?Sized = ValidContext> {
|
||||||
launch_fn: LaunchFn<Cfg>,
|
launch_fn: LaunchFn<Cfg, ContextFn>,
|
||||||
contexts: ContextList,
|
contexts: Vec<Box<ContextFn>>,
|
||||||
platform_config: Option<Cfg>,
|
platform_config: Option<Cfg>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type LaunchFn<Cfg> = fn(fn() -> Element, ContextList, Cfg);
|
pub type LaunchFn<Cfg, Context> = fn(fn() -> Element, Vec<Box<Context>>, Cfg);
|
||||||
|
|
||||||
// #[cfg(feature = "fullstack")]
|
#[cfg(feature = "fullstack")]
|
||||||
type ContextList = Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>;
|
type ValidContext = SendContext;
|
||||||
// #[cfg(not(feature = "fullstack"))]
|
|
||||||
// type ContextList = Vec<Box<dyn Fn() -> Box<dyn Any>>>;
|
#[cfg(not(feature = "fullstack"))]
|
||||||
|
type ValidContext = UnsendContext;
|
||||||
|
|
||||||
|
type SendContext = dyn Fn() -> Box<dyn Any> + Send + Sync + 'static;
|
||||||
|
|
||||||
|
type UnsendContext = dyn Fn() -> Box<dyn Any> + 'static;
|
||||||
|
|
||||||
impl LaunchBuilder {
|
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.
|
/// 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<current_platform::Config> {
|
pub fn new() -> LaunchBuilder<current_platform::Config, ValidContext> {
|
||||||
LaunchBuilder {
|
LaunchBuilder {
|
||||||
launch_fn: current_platform::launch,
|
launch_fn: current_platform::launch,
|
||||||
contexts: Vec::new(),
|
contexts: Vec::new(),
|
||||||
@@ -33,7 +38,7 @@ impl LaunchBuilder {
|
|||||||
|
|
||||||
/// Launch your web application.
|
/// Launch your web application.
|
||||||
#[cfg(feature = "web")]
|
#[cfg(feature = "web")]
|
||||||
pub fn web() -> LaunchBuilder<dioxus_web::Config> {
|
pub fn web() -> LaunchBuilder<dioxus_web::Config, UnsendContext> {
|
||||||
LaunchBuilder {
|
LaunchBuilder {
|
||||||
launch_fn: dioxus_web::launch::launch,
|
launch_fn: dioxus_web::launch::launch,
|
||||||
contexts: Vec::new(),
|
contexts: Vec::new(),
|
||||||
@@ -43,7 +48,7 @@ impl LaunchBuilder {
|
|||||||
|
|
||||||
/// Launch your desktop application.
|
/// Launch your desktop application.
|
||||||
#[cfg(feature = "desktop")]
|
#[cfg(feature = "desktop")]
|
||||||
pub fn desktop() -> LaunchBuilder<dioxus_desktop::Config> {
|
pub fn desktop() -> LaunchBuilder<dioxus_desktop::Config, UnsendContext> {
|
||||||
LaunchBuilder {
|
LaunchBuilder {
|
||||||
launch_fn: dioxus_desktop::launch::launch,
|
launch_fn: dioxus_desktop::launch::launch,
|
||||||
contexts: Vec::new(),
|
contexts: Vec::new(),
|
||||||
@@ -53,7 +58,7 @@ impl LaunchBuilder {
|
|||||||
|
|
||||||
/// Launch your fullstack application.
|
/// Launch your fullstack application.
|
||||||
#[cfg(feature = "fullstack")]
|
#[cfg(feature = "fullstack")]
|
||||||
pub fn fullstack() -> LaunchBuilder<dioxus_fullstack::Config> {
|
pub fn fullstack() -> LaunchBuilder<dioxus_fullstack::Config, SendContext> {
|
||||||
LaunchBuilder {
|
LaunchBuilder {
|
||||||
launch_fn: dioxus_fullstack::launch::launch,
|
launch_fn: dioxus_fullstack::launch::launch,
|
||||||
contexts: Vec::new(),
|
contexts: Vec::new(),
|
||||||
@@ -74,47 +79,35 @@ impl LaunchBuilder {
|
|||||||
/// Provide a custom launch function for your application.
|
/// 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.
|
/// Useful for third party renderers to tap into the launch builder API without having to reimplement it.
|
||||||
pub fn custom<Cfg>(launch_fn: LaunchFn<Cfg>) -> LaunchBuilder<Cfg> {
|
pub fn custom<Cfg, List>(launch_fn: LaunchFn<Cfg, List>) -> LaunchBuilder<Cfg, List> {
|
||||||
LaunchBuilder {
|
LaunchBuilder {
|
||||||
launch_fn,
|
launch_fn,
|
||||||
contexts: Vec::new(),
|
contexts: vec![],
|
||||||
platform_config: None,
|
platform_config: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fullstack platform builder
|
// Fullstack platform builder
|
||||||
impl<Cfg: Default> LaunchBuilder<Cfg> {
|
impl<Cfg: Default> LaunchBuilder<Cfg, ValidContext> {
|
||||||
#[cfg(feature = "fullstack")]
|
|
||||||
/// Inject state into the root component's context that is created on the thread that the app is launched on.
|
/// 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(
|
pub fn with_context_provider(
|
||||||
mut self,
|
mut self,
|
||||||
state: impl Fn() -> Box<dyn Any> + Send + Sync + 'static,
|
|
||||||
) -> Self {
|
|
||||||
self.contexts
|
|
||||||
.push(Box::new(state) as Box<dyn Fn() -> Box<dyn Any> + Send + Sync>);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "fullstack"))]
|
#[cfg(feature = "fullstack")] state: impl Fn() -> Box<dyn Any> + Send + Sync + 'static,
|
||||||
/// Inject state into the root component's context that is created on the thread that the app is launched on.
|
#[cfg(not(feature = "fullstack"))] state: impl Fn() -> Box<dyn Any> + 'static,
|
||||||
pub fn with_context_provider(mut self, state: impl Fn() -> Box<dyn Any> + 'static) -> Self {
|
) -> Self {
|
||||||
self.contexts
|
self.contexts.push(Box::new(state) as Box<ValidContext>);
|
||||||
.push(Box::new(state) as Box<dyn Fn() -> Box<dyn Any>>);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "fullstack")]
|
#[cfg(feature = "fullstack")]
|
||||||
/// Inject state into the root component's context.
|
/// Inject state into the root component's context.
|
||||||
pub fn with_context(mut self, state: impl Any + Clone + Send + Sync + 'static) -> Self {
|
pub fn with_context(
|
||||||
self.contexts
|
mut self,
|
||||||
.push(Box::new(move || Box::new(state.clone())));
|
#[cfg(feature = "fullstack")] state: impl Any + Clone + Send + Sync + 'static,
|
||||||
self
|
#[cfg(not(feature = "fullstack"))] state: impl Any + Clone + 'static,
|
||||||
}
|
) -> 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 {
|
|
||||||
self.contexts
|
self.contexts
|
||||||
.push(Box::new(move || Box::new(state.clone())));
|
.push(Box::new(move || Box::new(state.clone())));
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ fn main() {
|
|||||||
#[cfg(not(feature = "ssr"))]
|
#[cfg(not(feature = "ssr"))]
|
||||||
config.launch(|| {
|
config.launch(|| {
|
||||||
rsx! {
|
rsx! {
|
||||||
Router::<Route> {
|
Router::<Route> {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -31,6 +30,7 @@ fn main() {
|
|||||||
enum Route {
|
enum Route {
|
||||||
#[route("/")]
|
#[route("/")]
|
||||||
Home {},
|
Home {},
|
||||||
|
|
||||||
#[route("/blog/:id")]
|
#[route("/blog/:id")]
|
||||||
Blog { id: i32 },
|
Blog { id: i32 },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use std::any::Any;
|
|||||||
/// For a builder API, see `LaunchBuilder` defined in the `dioxus` crate.
|
/// For a builder API, see `LaunchBuilder` defined in the `dioxus` crate.
|
||||||
pub fn launch(
|
pub fn launch(
|
||||||
root: fn() -> Element,
|
root: fn() -> Element,
|
||||||
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
|
contexts: Vec<Box<dyn Fn() -> Box<dyn Any>>>,
|
||||||
platform_config: Config,
|
platform_config: Config,
|
||||||
) {
|
) {
|
||||||
let mut vdom = VirtualDom::new(root);
|
let mut vdom = VirtualDom::new(root);
|
||||||
|
|||||||
Reference in new issue
Block a user