fix: auto-reload
This commit is contained in:
1 parent
7aeddba427
commit
b617299860
3 files changed
+73
-62
No files matched your search
+2
-1
@@ -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"
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+70
-61
@@ -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,12 +20,33 @@ 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();
|
||||||
|
|
||||||
|
let (reload_tx, _) = broadcast::channel(100);
|
||||||
|
|
||||||
|
let ws_reload_state = Arc::new(WsRelodState {
|
||||||
|
update: reload_tx.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
// file watcher: check file change
|
// file watcher: check file change
|
||||||
let mut watcher = watcher(watcher_tx, Duration::from_secs(1)).unwrap();
|
let watcher_conf = config.clone();
|
||||||
|
let mut watcher = RecommendedWatcher::new(move |_| {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
let allow_watch_path = config
|
let allow_watch_path = config
|
||||||
.dioxus_config
|
.dioxus_config
|
||||||
.web
|
.web
|
||||||
@@ -40,46 +58,38 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
|
|||||||
for sub_path in allow_watch_path {
|
for sub_path in allow_watch_path {
|
||||||
watcher
|
watcher
|
||||||
.watch(
|
.watch(
|
||||||
config.crate_dir.join(sub_path),
|
&config.crate_dir.join(sub_path),
|
||||||
notify::RecursiveMode::Recursive,
|
notify::RecursiveMode::Recursive,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let (reload_tx, _) = broadcast::channel(100);
|
// tokio::spawn(async move {
|
||||||
|
// while let Ok(v) = watcher_rx.recv() {
|
||||||
let ws_reload_state = Arc::new(WsRelodState {
|
// match v {
|
||||||
update: reload_tx.clone(),
|
// DebouncedEvent::Create(_)
|
||||||
});
|
// | DebouncedEvent::Write(_)
|
||||||
|
// | DebouncedEvent::Remove(_)
|
||||||
let watcher_conf = config.clone();
|
// | DebouncedEvent::Rename(_, _) => {
|
||||||
|
// if builder::build(&watcher_conf).is_ok() {
|
||||||
tokio::spawn(async move {
|
// // change the websocket reload state to true;
|
||||||
while let Ok(v) = watcher_rx.recv() {
|
// // the page will auto-reload.
|
||||||
match v {
|
// if watcher_conf
|
||||||
DebouncedEvent::Create(_)
|
// .dioxus_config
|
||||||
| DebouncedEvent::Write(_)
|
// .web
|
||||||
| DebouncedEvent::Remove(_)
|
// .watcher
|
||||||
| DebouncedEvent::Rename(_, _) => {
|
// .reload_html
|
||||||
if builder::build(&watcher_conf).is_ok() {
|
// .unwrap_or(false)
|
||||||
// change the websocket reload state to true;
|
// {
|
||||||
// the page will auto-reload.
|
// let _ = Serve::regen_dev_page(&watcher_conf);
|
||||||
if watcher_conf
|
// }
|
||||||
.dioxus_config
|
// reload_tx.send("reload".into()).unwrap();
|
||||||
.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,25 +123,24 @@ 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);
|
||||||
if v == "reload" {
|
if v == "reload" {
|
||||||
// ignore the error
|
// ignore the error
|
||||||
if socket
|
if socket
|
||||||
.send(Message::Text(String::from("reload")))
|
.send(Message::Text(String::from("reload")))
|
||||||
.await
|
.await
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
println!("send failed");
|
break;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
reload_watcher.await.unwrap();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user