mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-18 06:58:10 -05:00
Debounce file change events
This commit is contained in:
parent
554301b8e9
commit
d4fa61e435
2 changed files with 82 additions and 17 deletions
|
@ -69,11 +69,11 @@ fn run_watch(
|
||||||
// Prevent dropping the guard until the end of the function.
|
// Prevent dropping the guard until the end of the function.
|
||||||
// Otherwise, the file watcher exits.
|
// Otherwise, the file watcher exits.
|
||||||
let _watcher_guard = if let Some(exercise_names) = notify_exercise_names {
|
let _watcher_guard = if let Some(exercise_names) = notify_exercise_names {
|
||||||
|
let notify_event_handler =
|
||||||
|
NotifyEventHandler::build(watch_event_sender.clone(), exercise_names)?;
|
||||||
|
|
||||||
let mut watcher = RecommendedWatcher::new(
|
let mut watcher = RecommendedWatcher::new(
|
||||||
NotifyEventHandler {
|
notify_event_handler,
|
||||||
sender: watch_event_sender.clone(),
|
|
||||||
exercise_names,
|
|
||||||
},
|
|
||||||
Config::default().with_poll_interval(Duration::from_secs(1)),
|
Config::default().with_poll_interval(Duration::from_secs(1)),
|
||||||
)
|
)
|
||||||
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
|
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
|
||||||
|
|
|
@ -1,15 +1,71 @@
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use notify::{
|
use notify::{
|
||||||
event::{MetadataKind, ModifyKind},
|
event::{AccessKind, AccessMode, MetadataKind, ModifyKind, RenameMode},
|
||||||
Event, EventKind,
|
Event, EventKind,
|
||||||
};
|
};
|
||||||
use std::sync::{atomic::Ordering::Relaxed, mpsc::Sender};
|
use std::{
|
||||||
|
sync::{
|
||||||
|
atomic::Ordering::Relaxed,
|
||||||
|
mpsc::{sync_channel, RecvTimeoutError, Sender, SyncSender},
|
||||||
|
},
|
||||||
|
thread,
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
use super::{WatchEvent, EXERCISE_RUNNING};
|
use super::{WatchEvent, EXERCISE_RUNNING};
|
||||||
|
|
||||||
|
const DEBOUNCE_DURATION: Duration = Duration::from_millis(200);
|
||||||
|
|
||||||
pub struct NotifyEventHandler {
|
pub struct NotifyEventHandler {
|
||||||
pub sender: Sender<WatchEvent>,
|
error_sender: Sender<WatchEvent>,
|
||||||
/// Used to report which exercise was modified.
|
// Sends the index of the updated exercise.
|
||||||
pub exercise_names: &'static [&'static [u8]],
|
update_sender: SyncSender<usize>,
|
||||||
|
// Used to report which exercise was modified.
|
||||||
|
exercise_names: &'static [&'static [u8]],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NotifyEventHandler {
|
||||||
|
pub fn build(
|
||||||
|
watch_event_sender: Sender<WatchEvent>,
|
||||||
|
exercise_names: &'static [&'static [u8]],
|
||||||
|
) -> Result<Self> {
|
||||||
|
let (update_sender, update_receiver) = sync_channel(0);
|
||||||
|
let error_sender = watch_event_sender.clone();
|
||||||
|
|
||||||
|
// Debouncer
|
||||||
|
thread::Builder::new()
|
||||||
|
.spawn(move || {
|
||||||
|
let mut exercise_updated = vec![false; exercise_names.len()];
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match update_receiver.recv_timeout(DEBOUNCE_DURATION) {
|
||||||
|
Ok(exercise_ind) => exercise_updated[exercise_ind] = true,
|
||||||
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
|
for (exercise_ind, updated) in exercise_updated.iter_mut().enumerate() {
|
||||||
|
if *updated {
|
||||||
|
if watch_event_sender
|
||||||
|
.send(WatchEvent::FileChange { exercise_ind })
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
*updated = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Disconnected) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.context("Failed to spawn a thread to debounce file changes")?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
error_sender,
|
||||||
|
update_sender,
|
||||||
|
exercise_names,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl notify::EventHandler for NotifyEventHandler {
|
impl notify::EventHandler for NotifyEventHandler {
|
||||||
|
@ -22,8 +78,8 @@ impl notify::EventHandler for NotifyEventHandler {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// An error occurs when the receiver is dropped.
|
// An error occurs when the receiver is dropped.
|
||||||
// After dropping the receiver, the debouncer guard should also be dropped.
|
// After dropping the receiver, the watcher guard should also be dropped.
|
||||||
let _ = self.sender.send(WatchEvent::NotifyErr(e));
|
let _ = self.error_sender.send(WatchEvent::NotifyErr(e));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -32,6 +88,10 @@ impl notify::EventHandler for NotifyEventHandler {
|
||||||
EventKind::Any => (),
|
EventKind::Any => (),
|
||||||
EventKind::Modify(modify_kind) => match modify_kind {
|
EventKind::Modify(modify_kind) => match modify_kind {
|
||||||
ModifyKind::Any | ModifyKind::Data(_) => (),
|
ModifyKind::Any | ModifyKind::Data(_) => (),
|
||||||
|
ModifyKind::Name(rename_mode) => match rename_mode {
|
||||||
|
RenameMode::Any | RenameMode::To => (),
|
||||||
|
RenameMode::From | RenameMode::Both | RenameMode::Other => return,
|
||||||
|
},
|
||||||
ModifyKind::Metadata(metadata_kind) => match metadata_kind {
|
ModifyKind::Metadata(metadata_kind) => match metadata_kind {
|
||||||
MetadataKind::Any | MetadataKind::WriteTime => (),
|
MetadataKind::Any | MetadataKind::WriteTime => (),
|
||||||
MetadataKind::AccessTime
|
MetadataKind::AccessTime
|
||||||
|
@ -40,12 +100,17 @@ impl notify::EventHandler for NotifyEventHandler {
|
||||||
| MetadataKind::Extended
|
| MetadataKind::Extended
|
||||||
| MetadataKind::Other => return,
|
| MetadataKind::Other => return,
|
||||||
},
|
},
|
||||||
ModifyKind::Name(_) | ModifyKind::Other => return,
|
ModifyKind::Other => return,
|
||||||
},
|
},
|
||||||
EventKind::Access(_)
|
EventKind::Access(access_kind) => match access_kind {
|
||||||
| EventKind::Create(_)
|
AccessKind::Any => (),
|
||||||
| EventKind::Remove(_)
|
AccessKind::Close(access_mode) => match access_mode {
|
||||||
| EventKind::Other => return,
|
AccessMode::Any | AccessMode::Write => (),
|
||||||
|
AccessMode::Execute | AccessMode::Read | AccessMode::Other => return,
|
||||||
|
},
|
||||||
|
AccessKind::Read | AccessKind::Open(_) | AccessKind::Other => return,
|
||||||
|
},
|
||||||
|
EventKind::Create(_) | EventKind::Remove(_) | EventKind::Other => return,
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = input_event
|
let _ = input_event
|
||||||
|
@ -62,6 +127,6 @@ impl notify::EventHandler for NotifyEventHandler {
|
||||||
.iter()
|
.iter()
|
||||||
.position(|exercise_name| *exercise_name == file_name_without_ext)
|
.position(|exercise_name| *exercise_name == file_name_without_ext)
|
||||||
})
|
})
|
||||||
.try_for_each(|exercise_ind| self.sender.send(WatchEvent::FileChange { exercise_ind }));
|
.try_for_each(|exercise_ind| self.update_sender.send(exercise_ind));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue