diff options
| author | Ivan Molodetskikh <yalterz@gmail.com> | 2025-02-12 20:53:19 +0300 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2025-02-12 20:56:32 +0300 |
| commit | eb8bd3894a188d34d870f4d79813f75163a318b8 (patch) | |
| tree | 3c8a4eabe2edb2e0402baf0418821d5ee9822d11 /src/utils | |
| parent | 7e552333a993e83a2dba52392109617e486f5f60 (diff) | |
| download | niri-eb8bd3894a188d34d870f4d79813f75163a318b8.tar.gz niri-eb8bd3894a188d34d870f4d79813f75163a318b8.tar.bz2 niri-eb8bd3894a188d34d870f4d79813f75163a318b8.zip | |
watcher: Allow running a processing function on the thread
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/watcher.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/utils/watcher.rs b/src/utils/watcher.rs index 84fdab5c..069e67f8 100644 --- a/src/utils/watcher.rs +++ b/src/utils/watcher.rs @@ -1,6 +1,6 @@ //! File modification watcher. -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{mpsc, Arc}; use std::thread; @@ -19,13 +19,18 @@ impl Drop for Watcher { } impl Watcher { - pub fn new(path: PathBuf, changed: SyncSender<()>) -> Self { - Self::with_start_notification(path, changed, None) + pub fn new<T: Send + 'static>( + path: PathBuf, + process: impl FnMut(&Path) -> T + Send + 'static, + changed: SyncSender<T>, + ) -> Self { + Self::with_start_notification(path, process, changed, None) } - pub fn with_start_notification( + pub fn with_start_notification<T: Send + 'static>( path: PathBuf, - changed: SyncSender<()>, + mut process: impl FnMut(&Path) -> T + Send + 'static, + changed: SyncSender<T>, started: Option<mpsc::SyncSender<()>>, ) -> Self { let should_stop = Arc::new(AtomicBool::new(false)); @@ -66,7 +71,9 @@ impl Watcher { if last_props.as_ref() != Some(&new_props) { trace!("file changed: {}", path.to_string_lossy()); - if let Err(err) = changed.send(()) { + let rv = process(&path); + + if let Err(err) = changed.send(rv) { warn!("error sending change notification: {err:?}"); break; } @@ -123,7 +130,8 @@ mod tests { let (tx, rx) = sync_channel(1); let (started_tx, started_rx) = mpsc::sync_channel(1); - let _watcher = Watcher::with_start_notification(config_path.clone(), tx, Some(started_tx)); + let _watcher = + Watcher::with_start_notification(config_path.clone(), |_| (), tx, Some(started_tx)); loop_handle .insert_source(rx, |_, _, _| { changed.fetch_add(1, Ordering::SeqCst); |
