factor out a hot reloading connect function

This commit is contained in:
= committed 2023-01-11 16:43:41 -06:00
1 parent e5e1abbdac
commit 042b67b61b
11 files changed
+47 -112

No files matched your search

-38
View File
@@ -1,38 +0,0 @@
#![allow(dead_code)]
use dioxus_core::Template;
use interprocess::local_socket::LocalSocketStream;
use std::io::{BufRead, BufReader};
use wry::application::{event_loop::EventLoopProxy, window::WindowId};
use crate::desktop_context::{EventData, UserWindowEvent};
pub(crate) fn init(proxy: EventLoopProxy<UserWindowEvent>) {
std::thread::spawn(move || {
let temp_file = std::env::temp_dir().join("@dioxusin");
if let Ok(socket) = LocalSocketStream::connect(temp_file.as_path()) {
let mut buf_reader = BufReader::new(socket);
loop {
let mut buf = String::new();
match buf_reader.read_line(&mut buf) {
Ok(_) => {
let template: Template<'static> =
serde_json::from_str(Box::leak(buf.into_boxed_str())).unwrap();
proxy
.send_event(UserWindowEvent(
EventData::TemplateUpdated(template),
unsafe { WindowId::dummy() },
))
.unwrap();
}
Err(err) => {
if err.kind() != std::io::ErrorKind::WouldBlock {
break;
}
}
}
}
}
});
}
+9 -4
View File
@@ -12,9 +12,6 @@ mod protocol;
mod waker;
mod webview;
#[cfg(all(feature = "hot-reload", debug_assertions))]
mod hot_reload;
pub use cfg::Config;
pub use desktop_context::{use_window, DesktopContext};
use desktop_context::{EventData, UserWindowEvent, WebviewQueue};
@@ -111,7 +108,15 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
// Intialize hot reloading if it is enabled
#[cfg(all(feature = "hot-reload", debug_assertions))]
hot_reload::init(proxy.clone());
{
let proxy = proxy.clone();
dioxus_hot_reload::connect(move |template| {
let _ = proxy.send_event(UserWindowEvent(
EventData::TemplateUpdated(template),
unsafe { WindowId::dummy() },
));
});
}
// We start the tokio runtime *on this thread*
// Any future we poll later will use this runtime to spawn tasks and for IO