mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 19:26:43 +01:00
first batch of dependency updates
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use notify::{DebouncedEvent, RecommendedWatcher, Watcher};
|
||||
use std::{collections::BTreeSet, path::PathBuf, sync::mpsc, time::Duration};
|
||||
use notify::{Event, EventKind, RecommendedWatcher, Watcher};
|
||||
use std::{collections::BTreeSet, path::PathBuf, sync::mpsc};
|
||||
|
||||
pub struct FileWatcher {
|
||||
watcher: RecommendedWatcher,
|
||||
watched_files: BTreeSet<PathBuf>,
|
||||
directories: BTreeSet<PathBuf>,
|
||||
rx: mpsc::Receiver<DebouncedEvent>,
|
||||
rx: mpsc::Receiver<Event>,
|
||||
}
|
||||
|
||||
impl FileWatcher {
|
||||
pub fn new() -> Result<FileWatcher> {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let watcher = notify::watcher(tx, Duration::from_millis(100))?;
|
||||
let watcher = notify::recommended_watcher(move |res| match res {
|
||||
Ok(event) => _ = tx.send(event),
|
||||
Err(err) => eprintln!("Error watching for file changes: {err}"),
|
||||
})?;
|
||||
Ok(FileWatcher {
|
||||
watcher,
|
||||
watched_files: BTreeSet::new(),
|
||||
@@ -36,16 +39,20 @@ impl FileWatcher {
|
||||
}
|
||||
|
||||
pub fn poll_changed_file(&self) -> Result<Option<PathBuf>> {
|
||||
let event = self.rx.try_recv();
|
||||
match event {
|
||||
Ok(DebouncedEvent::Create(path) | DebouncedEvent::Write(path)) => {
|
||||
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));
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
Err(mpsc::TryRecvError::Disconnected) => bail!("File watcher disconnected"),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user