1use dioxus_core::internal::HotReloadedTemplate;
2use dioxus_core::{ScopeId, VirtualDom};
3use dioxus_signals::{GlobalKey, Signal, Writable};
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().on_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 if msg.for_build_id == Some(dioxus_cli_config::build_id()) {
39 let our_pid = if cfg!(target_family = "wasm") {
40 None
41 } else {
42 Some(std::process::id())
43 };
44
45 if msg.for_pid == our_pid {
46 unsafe { subsecond::apply_patch(jump_table) }?;
47 dioxus_core::force_all_dirty();
48 ctx.clear::<Signal<Option<HotReloadedTemplate>>>();
49 }
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 if let Some(jumptable) = hot_reload_msg.jump_table {
80 if hot_reload_msg.for_pid == Some(std::process::id()) {
81 unsafe { subsecond::apply_patch(jumptable).unwrap() };
82 }
83 }
84 }
85 });
86}
87
88#[cfg(not(target_family = "wasm"))]
89pub fn connect_at(endpoint: String, mut callback: impl FnMut(DevserverMsg) + Send + 'static) {
90 std::thread::spawn(move || {
91 let uri = format!(
92 "{endpoint}?aslr_reference={}&build_id={}&pid={}",
93 subsecond::aslr_reference(),
94 dioxus_cli_config::build_id(),
95 std::process::id()
96 );
97
98 let (mut websocket, _req) = match tungstenite::connect(uri) {
99 Ok((websocket, req)) => (websocket, req),
100 Err(_) => return,
101 };
102
103 while let Ok(msg) = websocket.read() {
104 if let tungstenite::Message::Text(text) = msg {
105 if let Ok(msg) = serde_json::from_str(&text) {
106 callback(msg);
107 }
108 }
109 }
110 });
111}