Skip to main content

dioxus_devtools/
lib.rs

1use dioxus_core::internal::HotReloadedTemplate;
2use dioxus_core::{ScopeId, VirtualDom};
3use dioxus_signals::{GlobalKey, Signal, WritableExt};
4
5pub use dioxus_devtools_types::*;
6pub use subsecond;
7use subsecond::PatchError;
8
9/// Applies template and literal changes to the VirtualDom
10///
11/// Assets need to be handled by the renderer.
12pub fn apply_changes(dom: &VirtualDom, msg: &HotReloadMsg) {
13    try_apply_changes(dom, msg).unwrap()
14}
15
16/// Applies template and literal changes to the VirtualDom, but doesn't panic if patching fails.
17///
18/// Assets need to be handled by the renderer.
19pub fn try_apply_changes(dom: &VirtualDom, msg: &HotReloadMsg) -> Result<(), PatchError> {
20    dom.runtime().in_scope(ScopeId::ROOT, || {
21        // 1. Update signals...
22        let ctx = dioxus_signals::get_global_context();
23        for template in &msg.templates {
24            let value = template.template.clone();
25            let key = GlobalKey::File {
26                file: template.key.file.as_str(),
27                line: template.key.line as _,
28                column: template.key.column as _,
29                index: template.key.index as _,
30            };
31            if let Some(mut signal) = ctx.get_signal_with_key(key.clone()) {
32                signal.set(Some(value));
33            }
34        }
35
36        // 2. Attempt to hotpatch
37        if let Some(jump_table) = msg.jump_table.as_ref().cloned()
38            && msg.for_build_id == Some(dioxus_cli_config::build_id())
39        {
40            let our_pid = if cfg!(target_family = "wasm") {
41                None
42            } else {
43                Some(std::process::id())
44            };
45
46            if msg.for_pid == our_pid {
47                unsafe { subsecond::apply_patch(jump_table) }?;
48                dom.runtime().force_all_dirty();
49                ctx.clear::<Signal<Option<HotReloadedTemplate>>>();
50            }
51        }
52
53        Ok(())
54    })
55}
56
57/// Connect to the devserver and handle its messages with a callback.
58///
59/// This doesn't use any form of security or protocol, so it's not safe to expose to the internet.
60#[cfg(not(target_family = "wasm"))]
61pub fn connect(callback: impl FnMut(DevserverMsg) + Send + 'static) {
62    let Some(endpoint) = dioxus_cli_config::devserver_ws_endpoint() else {
63        return;
64    };
65
66    connect_at(endpoint, callback);
67}
68
69/// Connect to the devserver and handle hot-patch messages only, implementing the subsecond hotpatch
70/// protocol.
71///
72/// This is intended to be used by non-dioxus projects that want to use hotpatching.
73///
74/// To handle the full devserver protocol, use `connect` instead.
75#[cfg(not(target_family = "wasm"))]
76pub fn connect_subsecond() {
77    connect(|msg| {
78        if let DevserverMsg::HotReload(hot_reload_msg) = msg
79            && let Some(jumptable) = hot_reload_msg.jump_table
80            && hot_reload_msg.for_pid == Some(std::process::id())
81        {
82            unsafe { subsecond::apply_patch(jumptable).unwrap() };
83        }
84    });
85}
86
87#[cfg(not(target_family = "wasm"))]
88pub fn connect_at(endpoint: String, mut callback: impl FnMut(DevserverMsg) + Send + 'static) {
89    std::thread::spawn(move || {
90        let uri = format!(
91            "{endpoint}?aslr_reference={}&build_id={}&pid={}",
92            subsecond::aslr_reference(),
93            dioxus_cli_config::build_id(),
94            std::process::id()
95        );
96
97        let (mut websocket, _req) = match tungstenite::connect(uri) {
98            Ok((websocket, req)) => (websocket, req),
99            Err(_) => return,
100        };
101
102        while let Ok(msg) = websocket.read() {
103            if let tungstenite::Message::Text(text) = msg
104                && let Ok(msg) = serde_json::from_str(&text)
105            {
106                callback(msg);
107            }
108        }
109    });
110}
111
112/// Run this asynchronous future to completion.
113///
114/// Whenever your code changes, the future is dropped and a new one is created using the new function.
115///
116/// This is useful for using subsecond outside of dioxus, like with axum. To pass args to the underlying
117/// function, you can use the `serve_subsecond_with_args` function.
118///
119/// ```rust, ignore
120/// #[tokio::main]
121/// async fn main() {
122///     dioxus_devtools::serve_subsecond(router_main).await;
123/// }
124///
125/// async fn router_main() {
126///     use axum::{Router, routing::get};
127///
128///     let app = Router::new().route("/", get(test_route));
129///
130///     let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
131///     println!("Server running on http://localhost:3000");
132///
133///     axum::serve(listener, app.clone()).await.unwrap()
134/// }
135///
136/// async fn test_route() -> axum::response::Html<&'static str> {
137///     "axum works!!!!!".into()
138/// }
139/// ```
140#[cfg(feature = "serve")]
141#[cfg(not(target_family = "wasm"))]
142pub async fn serve_subsecond<O, F>(mut callback: impl FnMut() -> F)
143where
144    F: std::future::Future<Output = O> + 'static,
145{
146    serve_subsecond_with_args((), move |_args| callback()).await
147}
148
149/// Run this asynchronous future to completion.
150///
151/// Whenever your code changes, the future is dropped and a new one is created using the new function.
152///
153/// ```rust, ignore
154/// #[tokio::main]
155/// async fn main() {
156///     let args = ("abc".to_string(),);
157///     dioxus_devtools::serve_subsecond_with_args(args, router_main).await;
158/// }
159///
160/// async fn router_main(args: (String,)) {
161///     use axum::{Router, routing::get};
162///
163///     let app = Router::new().route("/", get(test_route));
164///
165///     let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
166///     println!("Server running on http://localhost:3000 -> {}", args.0);
167///
168///     axum::serve(listener, app.clone()).await.unwrap()
169/// }
170///
171/// async fn test_route() -> axum::response::Html<&'static str> {
172///     "axum works!!!!!".into()
173/// }
174/// ```
175#[cfg(feature = "serve")]
176pub async fn serve_subsecond_with_args<A: Clone, O, F>(args: A, mut callback: impl FnMut(A) -> F)
177where
178    F: std::future::Future<Output = O> + 'static,
179{
180    let (tx, mut rx) = futures_channel::mpsc::unbounded();
181
182    connect(move |msg| {
183        if let DevserverMsg::HotReload(hot_reload_msg) = msg
184            && let Some(jumptable) = hot_reload_msg.jump_table
185            && hot_reload_msg.for_pid == Some(std::process::id())
186        {
187            unsafe { subsecond::apply_patch(jumptable).unwrap() };
188            tx.unbounded_send(()).unwrap();
189        }
190    });
191
192    let wrapped = move |args| -> std::pin::Pin<Box<dyn std::future::Future<Output = O>>> {
193        Box::pin(callback(args))
194    };
195
196    let mut hotfn = subsecond::HotFn::current(wrapped);
197    let mut cur_future = hotfn.call((args.clone(),));
198
199    loop {
200        use futures_util::StreamExt;
201        let res = futures_util::future::select(cur_future, rx.next()).await;
202
203        match res {
204            futures_util::future::Either::Left(_completed) => _ = rx.next().await,
205            futures_util::future::Either::Right((None, callback)) => {
206                // Receiving `None` here means that the sender is not connected, which
207                // typically means the dioxus devtools protocol has never connected.
208                // We want to run the future to completion and return instead of
209                // re-running the future constantly in the loop.
210                callback.await;
211                return;
212            }
213            futures_util::future::Either::Right((Some(_), _)) => {}
214        }
215
216        cur_future = hotfn.call((args.clone(),));
217    }
218}