fix: high CPU usage

This commit is contained in:
mrxiaozhuox committed 2022-01-26 00:21:40 +08:00
1 parent 2c0b0460c9
commit 20977a49f0
2 files changed
+37 -43

No files matched your search

+6 -11
View File
@@ -5,21 +5,16 @@
var poll_interval = 2000; var poll_interval = 2000;
var reload_upon_connect = () => {
window.setTimeout(() => {
var ws = new WebSocket(url);
ws.onopen = () => window.location.reload();
ws.onclose = reload_upon_connect;
}, poll_interval);
};
var ws = new WebSocket(url); var ws = new WebSocket(url);
ws.onmessage = (ev) => { ws.addEventListener("message", (ev) => {
if (ev.data == "reload") { if (ev.data == "reload") {
window.location.reload(); window.location.reload();
} }
}; });
ws.addEventListener("open", () => {
ws.send("init");
});
ws.onclose = reload_upon_connect;
})(); })();
+31 -32
View File
@@ -20,8 +20,8 @@ struct WsRelodState {
} }
impl WsRelodState { impl WsRelodState {
fn change(&mut self) { fn change(&mut self, state: bool) {
self.update = !self.update; self.update = state;
} }
} }
@@ -34,40 +34,35 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
// file watcher: check file change // file watcher: check file change
let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap(); let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
watcher let allow_watch_path = config
.watch(config.crate_dir.clone(), notify::RecursiveMode::Recursive) .dioxus_config
.unwrap(); .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 ws_reload_state = Arc::new(Mutex::new(WsRelodState { update: false })); let ws_reload_state = Arc::new(Mutex::new(WsRelodState { update: false }));
let watcher_conf = config.clone(); let watcher_conf = config.clone();
let watcher_ws_state = ws_reload_state.clone(); let watcher_ws_state = ws_reload_state.clone();
tokio::spawn(async move { tokio::spawn(async move {
let allow_watch_path = watcher_conf
.dioxus_config
.web
.watcher
.watch_path
.clone()
.unwrap_or_else(|| vec![PathBuf::from("src")]);
let crate_dir = watcher_conf.crate_dir.clone();
loop { loop {
if let Ok(v) = rx.recv() { if let Ok(v) = rx.recv() {
match v { match v {
DebouncedEvent::Create(e) DebouncedEvent::Create(_)
| DebouncedEvent::Write(e) | DebouncedEvent::Write(_)
| DebouncedEvent::Remove(e) | DebouncedEvent::Remove(_)
| DebouncedEvent::Rename(e, _) => { | DebouncedEvent::Rename(_, _) => {
let mut should_reload = false; if builder::build(&watcher_conf).is_ok() {
for path in &allow_watch_path {
let temp = crate_dir.clone().join(path);
if e.starts_with(temp) {
should_reload = true;
break;
}
}
if should_reload && 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.
if watcher_conf if watcher_conf
@@ -79,7 +74,7 @@ pub async fn startup(config: CrateConfig) -> anyhow::Result<()> {
{ {
let _ = Serve::regen_dev_page(&watcher_conf); let _ = Serve::regen_dev_page(&watcher_conf);
} }
watcher_ws_state.lock().unwrap().change(); watcher_ws_state.lock().unwrap().change(true);
} }
} }
_ => {} _ => {}
@@ -118,13 +113,17 @@ async fn ws_handler(
) -> impl IntoResponse { ) -> impl IntoResponse {
ws.on_upgrade(|mut socket| async move { ws.on_upgrade(|mut socket| async move {
loop { loop {
tokio::time::sleep(tokio::time::Duration::from_millis(1200)).await; tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
if state.lock().unwrap().update { if state.lock().unwrap().update {
socket // ignore the error
if socket
.send(Message::Text(String::from("reload"))) .send(Message::Text(String::from("reload")))
.await .await
.unwrap(); .is_err()
state.lock().unwrap().change(); {
return;
}
state.lock().unwrap().change(false);
} }
} }
}) })