dioxus_web/
launch.rs

1//! This module contains the `launch` function, which is the main entry point for dioxus web
2
3pub use crate::Config;
4use dioxus_core::prelude::*;
5use std::any::Any;
6
7/// Launch the web application with the given root component, context and config
8///
9/// For a builder API, see `LaunchBuilder` defined in the `dioxus` crate.
10pub fn launch(
11    root: fn() -> Element,
12    contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
13    platform_config: Vec<Box<dyn Any>>,
14) {
15    let mut vdom = VirtualDom::new(root);
16    for context in contexts {
17        vdom.insert_any_root_context(context());
18    }
19
20    let platform_config = *platform_config
21        .into_iter()
22        .find_map(|cfg| cfg.downcast::<Config>().ok())
23        .unwrap_or_default();
24    launch_virtual_dom(vdom, platform_config)
25}
26
27/// Launch the web application with a prebuild virtual dom
28///
29/// For a builder API, see `LaunchBuilder` defined in the `dioxus` crate.
30pub fn launch_virtual_dom(vdom: VirtualDom, platform_config: Config) {
31    wasm_bindgen_futures::spawn_local(async move {
32        crate::run(vdom, platform_config).await;
33    });
34}
35
36/// Launch the web application with the given root component and config
37pub fn launch_cfg(root: fn() -> Element, platform_config: Config) {
38    launch(root, Vec::new(), vec![Box::new(platform_config)])
39}