Merge pull request #1697 from ealmloff/hot-reload-temp-dir

This commit is contained in:
Jonathan Kelley 2023-12-07 09:18:10 -08:00 committed by GitHub
commit fe7df0c6a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 19 deletions

View file

@ -43,8 +43,6 @@ pub async fn startup(config: CrateConfig) -> Result<()> {
let hot_reload_tx = broadcast::channel(100).0;
clear_paths();
Some(HotReloadState {
messages: hot_reload_tx.clone(),
file_map: file_map.clone(),
@ -110,7 +108,14 @@ pub async fn serve(config: CrateConfig, hot_reload_state: Option<HotReloadState>
}
async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<()> {
match LocalSocketListener::bind("@dioxusin") {
let metadata = cargo_metadata::MetadataCommand::new()
.no_deps()
.exec()
.unwrap();
let target_dir = metadata.target_directory.as_std_path();
let path = target_dir.join("dioxusin");
clear_paths(&path);
match LocalSocketListener::bind(path) {
Ok(local_socket_stream) => {
let aborted = Arc::new(Mutex::new(false));
// States
@ -182,17 +187,14 @@ async fn start_desktop_hot_reload(hot_reload_state: HotReloadState) -> Result<()
Ok(())
}
fn clear_paths() {
fn clear_paths(file_socket_path: &std::path::Path) {
if cfg!(target_os = "macos") {
// On unix, if you force quit the application, it can leave the file socket open
// This will cause the local socket listener to fail to open
// We check if the file socket is already open from an old session and then delete it
let paths = ["./dioxusin", "./@dioxusin"];
for path in paths {
let path = std::path::PathBuf::from(path);
if path.exists() {
let _ = std::fs::remove_file(path);
}
if file_socket_path.exists() {
let _ = std::fs::remove_file(file_socket_path);
}
}
}

View file

@ -152,21 +152,20 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
}
let file_map = Arc::new(Mutex::new(file_map));
let target_dir = crate_dir.join("target");
let hot_reload_socket_path = target_dir.join("dioxusin");
#[cfg(target_os = "macos")]
{
// On unix, if you force quit the application, it can leave the file socket open
// This will cause the local socket listener to fail to open
// We check if the file socket is already open from an old session and then delete it
let paths = ["./dioxusin", "./@dioxusin"];
for path in paths {
let path = PathBuf::from(path);
if path.exists() {
let _ = std::fs::remove_file(path);
}
if hot_reload_socket_path.exists() {
let _ = std::fs::remove_file(hot_reload_socket_path);
}
}
match LocalSocketListener::bind("@dioxusin") {
match LocalSocketListener::bind(hot_reload_socket_path) {
Ok(local_socket_stream) => {
let aborted = Arc::new(Mutex::new(false));

View file

@ -1,4 +1,7 @@
use std::io::{BufRead, BufReader};
use std::{
io::{BufRead, BufReader},
path::PathBuf,
};
use dioxus_core::Template;
#[cfg(feature = "file_watcher")]
@ -24,7 +27,8 @@ pub enum HotReloadMsg {
/// 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) {
std::thread::spawn(move || {
if let Ok(socket) = LocalSocketStream::connect("@dioxusin") {
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();