use dioxus_core::{ScopeId, VirtualDom};
pub use dioxus_devtools_types::*;
use dioxus_signals::{GlobalKey, Writable};
use warnings::Warning;
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()) {
dioxus_signals::warnings::signal_read_and_write_in_reactive_scope::allow(|| {
dioxus_signals::warnings::signal_write_in_component_body::allow(|| {
signal.set(Some(value));
});
});
}
}
});
}
#[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);
}
}
}
});
}