fix hotreloading issues in the CLI

This commit is contained in:
Jonathan Kelley committed 2024-03-12 13:39:42 -07:00
1 parent d180f569cf
commit ad7a350d2e
37 files changed
+818 -805

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ use dioxus_rsx::{
hot_reload::{FileMap, FileMapBuildResult, UpdateResult},
HotReloadingContext,
};
use interprocess_docfix::local_socket::LocalSocketListener;
use interprocess::local_socket::LocalSocketListener;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
#[cfg(feature = "file_watcher")]
+25 -18
View File
@@ -6,7 +6,7 @@ use std::{
use dioxus_core::Template;
#[cfg(feature = "file_watcher")]
pub use dioxus_html::HtmlCtx;
use interprocess_docfix::local_socket::LocalSocketStream;
use interprocess::local_socket::LocalSocketStream;
use serde::{Deserialize, Serialize};
#[cfg(feature = "custom_file_watcher")]
@@ -15,36 +15,43 @@ mod file_watcher;
pub use file_watcher::*;
/// A message the hot reloading server sends to the client
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(bound(deserialize = "'de: 'static"))]
pub enum HotReloadMsg {
/// A template has been updated
UpdateTemplate(Template),
/// A template has been updated
UpdateAsset(PathBuf),
/// The program needs to be recompiled, and the client should shut down
Shutdown,
}
/// Connect to the hot reloading listener. The callback provided will be called every time a template change is detected
pub fn connect(mut f: impl FnMut(HotReloadMsg) + Send + 'static) {
pub fn connect(mut callback: impl FnMut(HotReloadMsg) + Send + 'static) {
std::thread::spawn(move || {
let path = PathBuf::from("./").join("target").join("dioxusin");
if let Ok(socket) = LocalSocketStream::connect(path) {
let mut buf_reader = BufReader::new(socket);
loop {
let mut buf = String::new();
match buf_reader.read_line(&mut buf) {
Ok(_) => {
let template: HotReloadMsg =
serde_json::from_str(Box::leak(buf.into_boxed_str())).unwrap();
f(template);
}
Err(err) => {
if err.kind() != std::io::ErrorKind::WouldBlock {
break;
}
}
let socket =
LocalSocketStream::connect(path).expect("Could not connect to hot reloading server.");
let mut buf_reader = BufReader::new(socket);
loop {
let mut buf = String::new();
if let Err(err) = buf_reader.read_line(&mut buf) {
if err.kind() != std::io::ErrorKind::WouldBlock {
break;
}
}
let template = serde_json::from_str(Box::leak(buf.into_boxed_str())).expect(
"Could not parse hot reloading message - make sure your client is up to date",
);
callback(template);
}
});
}