From bbfb5eba49dc6df3c72fb15cd1a9813567408161 Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Mon, 30 Jan 2023 00:09:25 +0100 Subject: [PATCH] add back event debouncing in file watcher --- Cargo.lock | 13 +++++++++++-- Cargo.toml | 2 +- src/filewatcher.rs | 43 +++++++++++++++++++++++++++---------------- test/ges_test.cwa | 5 +++-- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8a76dad..b5c90d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2069,6 +2069,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "notify-debouncer-mini" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e23e9fa24f094b143c1eb61f90ac6457de87be6987bc70746e0179f7dbc9007b" +dependencies = [ + "notify", +] + [[package]] name = "num-complex" version = "0.4.3" @@ -3374,7 +3383,7 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] name = "uw8" -version = "0.2.1" +version = "0.2.2" dependencies = [ "ansi_term", "anyhow", @@ -3382,7 +3391,7 @@ dependencies = [ "curlywas", "env_logger", "log", - "notify", + "notify-debouncer-mini", "pico-args 0.5.0", "rubato", "same-file", diff --git a/Cargo.toml b/Cargo.toml index c14a48c..1d7086a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ anyhow = "1" env_logger = "0.10" log = "0.4" uw8-window = { path = "uw8-window", optional = true } -notify = "5" +notify-debouncer-mini = { version = "0.2.1", default-features = false } pico-args = "0.5" curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "0e7ea50" } wat = "1" diff --git a/src/filewatcher.rs b/src/filewatcher.rs index 60deacd..3f27c0b 100644 --- a/src/filewatcher.rs +++ b/src/filewatcher.rs @@ -1,23 +1,35 @@ use anyhow::{anyhow, bail, Result}; -use notify::{Event, EventKind, RecommendedWatcher, Watcher}; -use std::{collections::BTreeSet, path::PathBuf, sync::mpsc}; +use notify_debouncer_mini::{ + new_debouncer, + notify::{self, RecommendedWatcher}, + DebouncedEvent, DebouncedEventKind, Debouncer, +}; +use std::{collections::BTreeSet, path::PathBuf, sync::mpsc, time::Duration}; pub struct FileWatcher { - watcher: RecommendedWatcher, + debouncer: Debouncer, watched_files: BTreeSet, directories: BTreeSet, - rx: mpsc::Receiver, + rx: mpsc::Receiver, } impl FileWatcher { pub fn new() -> Result { let (tx, rx) = mpsc::channel(); - let watcher = notify::recommended_watcher(move |res| match res { - Ok(event) => _ = tx.send(event), - Err(err) => eprintln!("Error watching for file changes: {err}"), + let debouncer = new_debouncer(Duration::from_millis(100), None, move |res| match res { + Ok(events) => { + for event in events { + let _ = tx.send(event); + } + } + Err(errs) => { + for err in errs { + eprintln!("Error watching for file changes: {err}"); + } + } })?; Ok(FileWatcher { - watcher, + debouncer, watched_files: BTreeSet::new(), directories: BTreeSet::new(), rx, @@ -29,7 +41,8 @@ impl FileWatcher { let parent = path.parent().ok_or_else(|| anyhow!("File has no parent"))?; if !self.directories.contains(parent) { - self.watcher + self.debouncer + .watcher() .watch(parent, notify::RecursiveMode::NonRecursive)?; self.directories.insert(parent.to_path_buf()); } @@ -41,13 +54,11 @@ impl FileWatcher { pub fn poll_changed_file(&self) -> Result> { match self.rx.try_recv() { Ok(event) => match event.kind { - EventKind::Create(_) | EventKind::Modify(_) => { - for path in event.paths { - let handle = same_file::Handle::from_path(&path)?; - for file in &self.watched_files { - if handle == same_file::Handle::from_path(file)? { - return Ok(Some(path)); - } + DebouncedEventKind::Any => { + let handle = same_file::Handle::from_path(&event.path)?; + for file in &self.watched_files { + if handle == same_file::Handle::from_path(file)? { + return Ok(Some(event.path)); } } } diff --git a/test/ges_test.cwa b/test/ges_test.cwa index 34cde63..b098621 100644 --- a/test/ges_test.cwa +++ b/test/ges_test.cwa @@ -2,17 +2,18 @@ import "env.memory" memory(4); import "env.pow" fn pow(f32, f32) -> f32; import "env.sin" fn sin(f32) -> f32; import "env.cls" fn cls(i32); +import "env.exp" fn exp(f32) -> f32; import "env.rectangle" fn rectangle(f32, f32, f32, f32, i32); include "../platform/src/ges.cwa" export fn snd(t: i32) -> f32 { - gesSnd(t) + sndGes(t) } export fn upd() { 80?0 = 32!32 / 200 & 2 | 0x41; - 80?3 = (32!32 / 400)%7*12/7 + 40; + 80?3 = (32!32 / 400)%8*12/7 + 40; let pulse = (32!32 * 256 / 2000) & 511; if pulse >= 256 { pulse = 511 - pulse;