summaryrefslogtreecommitdiff
path: root/src/watch.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-09-18 01:43:48 +0200
committermo8it <mo8it@proton.me>2024-09-18 01:43:48 +0200
commit89c40ba25672b2da17e2fcc5bc742462699d54dd (patch)
treef46f89010e2cd19034db5c69d4dc5ffb6fb0f6c3 /src/watch.rs
parente56ae6d65144d0a0bc8cc6759c89e59658a71497 (diff)
Optimize the file watcher
Diffstat (limited to 'src/watch.rs')
-rw-r--r--src/watch.rs43
1 files changed, 32 insertions, 11 deletions
diff --git a/src/watch.rs b/src/watch.rs
index c937bfb..fd89b29 100644
--- a/src/watch.rs
+++ b/src/watch.rs
@@ -1,12 +1,12 @@
use anyhow::{Error, Result};
-use notify_debouncer_mini::{
- new_debouncer,
- notify::{self, RecursiveMode},
-};
+use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::{
io::{self, Write},
path::Path,
- sync::mpsc::channel,
+ sync::{
+ atomic::{AtomicBool, Ordering::Relaxed},
+ mpsc::channel,
+ },
time::Duration,
};
@@ -21,6 +21,27 @@ mod notify_event;
mod state;
mod terminal_event;
+static EXERCISE_RUNNING: AtomicBool = AtomicBool::new(false);
+
+// Private unit type to force using the constructor function.
+#[must_use = "When the guard is dropped, the input is unpaused"]
+pub struct InputPauseGuard(());
+
+impl InputPauseGuard {
+ #[inline]
+ pub fn scoped_pause() -> Self {
+ EXERCISE_RUNNING.store(true, Relaxed);
+ Self(())
+ }
+}
+
+impl Drop for InputPauseGuard {
+ #[inline]
+ fn drop(&mut self) {
+ EXERCISE_RUNNING.store(false, Relaxed);
+ }
+}
+
enum WatchEvent {
Input(InputEvent),
FileChange { exercise_ind: usize },
@@ -47,21 +68,21 @@ fn run_watch(
let mut manual_run = false;
// Prevent dropping the guard until the end of the function.
// Otherwise, the file watcher exits.
- let _debouncer_guard = if let Some(exercise_names) = notify_exercise_names {
- let mut debouncer = new_debouncer(
- Duration::from_millis(200),
+ let _watcher_guard = if let Some(exercise_names) = notify_exercise_names {
+ let mut watcher = RecommendedWatcher::new(
NotifyEventHandler {
sender: watch_event_sender.clone(),
exercise_names,
},
+ Config::default().with_poll_interval(Duration::from_secs(1)),
)
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
- debouncer
- .watcher()
+
+ watcher
.watch(Path::new("exercises"), RecursiveMode::Recursive)
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
- Some(debouncer)
+ Some(watcher)
} else {
manual_run = true;
None