Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback on PollWatcher #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ lazy_static = "1.4.0"
notify = "6.1.1"
regex = "1.7.1"
ratatui = "0.23.0"
tempfile = "3.8.0"
55 changes: 50 additions & 5 deletions src/file_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crossbeam::{
select,
};
use notify::{event::ModifyKind, RecursiveMode, Watcher};
use tempfile::NamedTempFile;

use crate::app::AppMessage;

Expand Down Expand Up @@ -70,7 +71,10 @@ impl FileWatcher {
fn run(&mut self) -> Result<(), RecvError> {
let (watch_sender, watch_receiver) = unbounded();
let mut watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
let event = res.unwrap();
let event = match res {
Ok(e) => e,
Err(_) => return,
};
match event.kind {
notify::EventKind::Modify(ModifyKind::Data(_)) => {
watch_sender.send(event.paths).unwrap();
Expand All @@ -80,6 +84,49 @@ impl FileWatcher {
})
.unwrap();

// Check if the file watcher limit is reached by creating a file, watching it and then deleting it
let tmp_file = NamedTempFile::new().unwrap();
let tmp_file_path = tmp_file.path();
let watcher_result = watcher.watch(tmp_file_path, RecursiveMode::NonRecursive);
let max_watches_reached = match watcher_result {
Err(notify::Error {
kind: notify::ErrorKind::MaxFilesWatch,
..
}) => true,
Ok(_) => {
watcher.unwatch(tmp_file_path).unwrap();
false
}
_ => false,
};
let _ = tmp_file.close();

let config = notify::Config::default()
.with_poll_interval(Duration::from_secs(2))
.with_compare_contents(false);

let mut watcher = if max_watches_reached {
let (watch_sender, _) = unbounded();
let watcher = notify::PollWatcher::new(
move |res: notify::Result<notify::Event>| {
let event = match res {
Ok(e) => e,
Err(_) => return,
};
match event.kind {
notify::EventKind::Modify(ModifyKind::Data(_)) => {
let _ = watch_sender.send(event.paths);
}
_ => {}
};
},
config,
);
Box::new(watcher.unwrap()) as Box<dyn Watcher>
} else {
Box::new(watcher) as Box<dyn Watcher>
};

let (mut _content_sender, mut _content_receiver) = unbounded::<io::Result<String>>();
let (mut _watch_sender, mut _watch_receiver) = unbounded::<()>();
loop {
Expand All @@ -91,7 +138,7 @@ impl FileWatcher {
(_watch_sender, _watch_receiver) = unbounded::<()>();

if let Some(p) = &self.file_path {
watcher.unwatch(p).expect(format!("Failed to unwatch {:?}", p).as_str());
let _ = watcher.unwatch(p);
self.file_path = None;
}

Expand Down Expand Up @@ -177,9 +224,7 @@ impl FileWatcherHandle {
pub fn set_file_path(&mut self, file_path: Option<PathBuf>) {
if self.file_path != file_path {
self.file_path = file_path.clone();
self.sender
.send(FileWatcherMessage::FilePath(file_path))
.unwrap();
let _ = self.sender.send(FileWatcherMessage::FilePath(file_path));
}
}
}