launch.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //! This module contains the `launch` function, which is the main entry point for dioxus fullstack
  2. use std::any::Any;
  3. use dioxus_lib::prelude::{Element, VirtualDom};
  4. pub use crate::Config;
  5. /// Launch a fullstack app with the given root component, contexts, and config.
  6. pub fn launch(
  7. root: fn() -> Element,
  8. contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
  9. platform_config: Config,
  10. ) {
  11. let virtual_dom_factory = move || {
  12. let mut vdom = VirtualDom::new(root);
  13. for context in &contexts {
  14. vdom.insert_any_root_context(context());
  15. }
  16. vdom
  17. };
  18. #[cfg(feature = "ssr")]
  19. tokio::runtime::Runtime::new()
  20. .unwrap()
  21. .block_on(async move {
  22. platform_config.launch_server(virtual_dom_factory).await;
  23. });
  24. #[cfg(not(feature = "ssr"))]
  25. {
  26. #[cfg(feature = "web")]
  27. {
  28. // TODO: this should pull the props from the document
  29. let cfg = platform_config.web_cfg.hydrate(true);
  30. dioxus_web::launch::launch_virtual_dom(virtual_dom_factory(), cfg);
  31. }
  32. #[cfg(feature = "desktop")]
  33. {
  34. let cfg = platform_config.desktop_cfg;
  35. dioxus_desktop::launch::launch_virtual_dom(virtual_dom_factory(), cfg)
  36. }
  37. #[cfg(feature = "mobile")]
  38. {
  39. let cfg = platform_config.mobile_cfg;
  40. dioxus_mobile::launch::launch_virtual_dom(virtual_dom_factory(), cfg)
  41. }
  42. }
  43. }