Prechádzať zdrojové kódy

fix: dev server `auto-reload`

mrxiaozhuox 3 rokov pred
rodič
commit
b8b0303904
2 zmenil súbory, kde vykonal 9 pridanie a 4 odobranie
  1. 1 1
      Cargo.toml
  2. 8 3
      src/server/mod.rs

+ 1 - 1
Cargo.toml

@@ -20,7 +20,6 @@ toml = "0.5.8"
 fs_extra = "1.2.0"
 cargo_toml = "0.10.0"
 futures = "0.3.12"
-# notify = "4.0.17"
 notify = { version = "5.0.0-pre.13", features = ["serde"] }
 html_parser = "0.6.2"
 binary-install = "0.0.2"
@@ -30,6 +29,7 @@ cargo_metadata = "0.14.1"
 tokio = { version = "1.15.0", features = ["full"] }
 atty = "0.2.14"
 regex = "1.5.4"
+chrono = "0.4.19"
 
 axum = { version = "0.4.4", features = ["ws", "headers"] }
 tower-http = { version = "0.2.0", features = ["fs", "trace"] }

+ 8 - 3
src/server/mod.rs

@@ -28,10 +28,14 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
         update: reload_tx.clone(),
     });
 
+    let mut last_update_time = chrono::Local::now().timestamp();
+
     // file watcher: check file change
     let watcher_conf = config.clone();
-    let mut watcher = RecommendedWatcher::new(move |_| {
-        if builder::build(&watcher_conf).is_ok() {
+    let mut watcher = RecommendedWatcher::new(move | _ : notify::Result<notify::Event>| {
+        if chrono::Local::now().timestamp() > last_update_time
+            && builder::build(&watcher_conf).is_ok()
+        {
             // change the websocket reload state to true;
             // the page will auto-reload.
             if watcher_conf
@@ -43,7 +47,9 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
             {
                 let _ = Serve::regen_dev_page(&watcher_conf);
             }
+            println!("watcher send reload");
             reload_tx.send("reload".into()).unwrap();
+            last_update_time = chrono::Local::now().timestamp();
         }
     })
     .unwrap();
@@ -127,7 +133,6 @@ async fn ws_handler(
         let reload_watcher = tokio::spawn(async move {
             loop {
                 let v = rx.recv().await.unwrap();
-                println!("trigger recv: {}", v);
                 if v == "reload" {
                     // ignore the error
                     if socket