fix: auto-reload

This commit is contained in:
mrxiaozhuox committed 2022-01-27 11:13:41 +08:00
1 parent 7aeddba427
commit b617299860
3 files changed
+57 -46

No files matched your search

+2 -1
View File
@@ -20,7 +20,8 @@ toml = "0.5.8"
fs_extra = "1.2.0" fs_extra = "1.2.0"
cargo_toml = "0.10.0" cargo_toml = "0.10.0"
futures = "0.3.12" futures = "0.3.12"
notify = "4.0.17" # notify = "4.0.17"
notify = { version = "5.0.0-pre.13", features = ["serde"] }
html_parser = "0.6.2" html_parser = "0.6.2"
binary-install = "0.0.2" binary-install = "0.0.2"
convert_case = "0.5.0" convert_case = "0.5.0"
+1
View File
@@ -15,6 +15,7 @@
var ws = new WebSocket(url); var ws = new WebSocket(url);
ws.onmessage = (ev) => { ws.onmessage = (ev) => {
if (ev.data == "reload") { if (ev.data == "reload") {
// alert("reload!!!");
window.location.reload(); window.location.reload();
} }
}; };
+54 -45
View File
@@ -5,12 +5,9 @@ use axum::{
routing::{get, get_service}, routing::{get, get_service},
AddExtensionLayer, Router, AddExtensionLayer, Router,
}; };
use notify::{watcher, DebouncedEvent, Watcher}; use notify::{RecommendedWatcher, Watcher};
use std::time::Duration;
use std::{ use std::{path::PathBuf, sync::Arc};
path::PathBuf,
sync::{mpsc::channel, Arc},
};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use crate::{builder, serve::Serve, CrateConfig}; use crate::{builder, serve::Serve, CrateConfig};
@@ -23,44 +20,17 @@ struct WsRelodState {
pub async fn startup(config: CrateConfig) -> anyhow::Result<()> { pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
log::info!("🚀 Starting development server..."); log::info!("🚀 Starting development server...");
let (watcher_tx, watcher_rx) = channel();
let dist_path = config.out_dir.clone(); let dist_path = config.out_dir.clone();
// file watcher: check file change
let mut watcher = watcher(watcher_tx, Duration::from_secs(1)).unwrap();
let allow_watch_path = config
.dioxus_config
.web
.watcher
.watch_path
.clone()
.unwrap_or_else(|| vec![PathBuf::from("src")]);
for sub_path in allow_watch_path {
watcher
.watch(
config.crate_dir.join(sub_path),
notify::RecursiveMode::Recursive,
)
.unwrap();
}
let (reload_tx, _) = broadcast::channel(100); let (reload_tx, _) = broadcast::channel(100);
let ws_reload_state = Arc::new(WsRelodState { let ws_reload_state = Arc::new(WsRelodState {
update: reload_tx.clone(), update: reload_tx.clone(),
}); });
// file watcher: check file change
let watcher_conf = config.clone(); let watcher_conf = config.clone();
let mut watcher = RecommendedWatcher::new(move |_| {
tokio::spawn(async move {
while let Ok(v) = watcher_rx.recv() {
match v {
DebouncedEvent::Create(_)
| DebouncedEvent::Write(_)
| DebouncedEvent::Remove(_)
| DebouncedEvent::Rename(_, _) => {
if builder::build(&watcher_conf).is_ok() { if builder::build(&watcher_conf).is_ok() {
// change the websocket reload state to true; // change the websocket reload state to true;
// the page will auto-reload. // the page will auto-reload.
@@ -75,11 +45,51 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
} }
reload_tx.send("reload".into()).unwrap(); reload_tx.send("reload".into()).unwrap();
} }
})
.unwrap();
let allow_watch_path = config
.dioxus_config
.web
.watcher
.watch_path
.clone()
.unwrap_or_else(|| vec![PathBuf::from("src")]);
for sub_path in allow_watch_path {
watcher
.watch(
&config.crate_dir.join(sub_path),
notify::RecursiveMode::Recursive,
)
.unwrap();
} }
_ => {}
} // tokio::spawn(async move {
} // while let Ok(v) = watcher_rx.recv() {
}); // match v {
// DebouncedEvent::Create(_)
// | DebouncedEvent::Write(_)
// | DebouncedEvent::Remove(_)
// | DebouncedEvent::Rename(_, _) => {
// if builder::build(&watcher_conf).is_ok() {
// // change the websocket reload state to true;
// // the page will auto-reload.
// if watcher_conf
// .dioxus_config
// .web
// .watcher
// .reload_html
// .unwrap_or(false)
// {
// let _ = Serve::regen_dev_page(&watcher_conf);
// }
// reload_tx.send("reload".into()).unwrap();
// }
// }
// _ => {}
// }
// }
// });
// start serve dev-server at 0.0.0.0:8080 // start serve dev-server at 0.0.0.0:8080
let port = "8080"; let port = "8080";
@@ -113,11 +123,8 @@ async fn ws_handler(
Extension(state): Extension<Arc<WsRelodState>>, Extension(state): Extension<Arc<WsRelodState>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
ws.on_upgrade(|mut socket| async move { ws.on_upgrade(|mut socket| async move {
log::info!("New websocket conenct.");
let mut rx = state.update.subscribe(); let mut rx = state.update.subscribe();
let reload_watcher = tokio::spawn(async move {
loop { loop {
let v = rx.recv().await.unwrap(); let v = rx.recv().await.unwrap();
println!("trigger recv: {}", v); println!("trigger recv: {}", v);
@@ -128,10 +135,12 @@ async fn ws_handler(
.await .await
.is_err() .is_err()
{ {
println!("send failed"); break;
return;
} }
} }
} }
});
reload_watcher.await.unwrap();
}) })
} }