use dioxus_core::{ScopeId, VirtualDom}; pub use dioxus_devtools_types::*; use dioxus_signals::{GlobalKey, Writable}; /// Applies template and literal changes to the VirtualDom /// /// Assets need to be handled by the renderer. pub fn apply_changes(dom: &VirtualDom, msg: &HotReloadMsg) { dom.runtime().on_scope(ScopeId::ROOT, || { let ctx = dioxus_signals::get_global_context(); for template in &msg.templates { let value = template.template.clone(); let key = GlobalKey::File { file: template.key.file.as_str(), line: template.key.line as _, column: template.key.column as _, index: template.key.index as _, }; if let Some(mut signal) = ctx.get_signal_with_key(key.clone()) { signal.set(Some(value)); } } }); } /// Connect to the devserver and handle its messages with a callback. /// /// This doesn't use any form of security or protocol, so it's not safe to expose to the internet. #[cfg(not(target_arch = "wasm32"))] pub fn connect(endpoint: String, mut callback: impl FnMut(DevserverMsg) + Send + 'static) { std::thread::spawn(move || { let (mut websocket, _req) = match tungstenite::connect(endpoint.clone()) { Ok((websocket, req)) => (websocket, req), Err(_) => return, }; while let Ok(msg) = websocket.read() { if let tungstenite::Message::Text(text) = msg { if let Ok(msg) = serde_json::from_str(&text) { callback(msg); } } } }); }