dioxus_web/
launch.rs

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