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
9pub fn apply_changes(dom: &VirtualDom, msg: &HotReloadMsg) {
13 try_apply_changes(dom, msg).unwrap()
14}
15
16pub fn try_apply_changes(dom: &VirtualDom, msg: &HotReloadMsg) -> Result<(), PatchError> {
20 dom.runtime().in_scope(ScopeId::ROOT, || {
21 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 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#[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#[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#[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#[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 callback.await;
211 return;
212 }
213 futures_util::future::Either::Right((Some(_), _)) => {}
214 }
215
216 cur_future = hotfn.call((args.clone(),));
217 }
218}