fix(core): fix race condition when multiple files are changed
This commit is contained in:
parent
55c8ba6b75
commit
c9e8f3e480
|
@ -17,7 +17,10 @@
|
||||||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::{path::Path, time::Duration};
|
use std::{
|
||||||
|
path::Path,
|
||||||
|
time::{Duration, Instant},
|
||||||
|
};
|
||||||
|
|
||||||
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
||||||
|
|
||||||
|
@ -52,6 +55,8 @@ fn watcher_main(config_dir: &Path, watcher_notify: &Sender<()>) {
|
||||||
|
|
||||||
info!("watching for changes in path: {:?}", config_dir);
|
info!("watching for changes in path: {:?}", config_dir);
|
||||||
|
|
||||||
|
let mut last_event_arrival = Instant::now();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let should_reload = match rx.recv() {
|
let should_reload = match rx.recv() {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
|
@ -73,6 +78,9 @@ fn watcher_main(config_dir: &Path, watcher_notify: &Sender<()>) {
|
||||||
if ["yml", "yaml"].iter().any(|ext| ext == &extension) {
|
if ["yml", "yaml"].iter().any(|ext| ext == &extension) {
|
||||||
// Only load non-hidden yml files
|
// Only load non-hidden yml files
|
||||||
!is_file_hidden(&path)
|
!is_file_hidden(&path)
|
||||||
|
} else if extension == "" {
|
||||||
|
// No extension, probably a folder
|
||||||
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -86,11 +94,16 @@ fn watcher_main(config_dir: &Path, watcher_notify: &Sender<()>) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if should_reload {
|
// Send only one event, otherwise we could run the risk of useless reloads or even race conditions.
|
||||||
|
if should_reload
|
||||||
|
&& last_event_arrival.elapsed() > std::time::Duration::from_secs(WATCHER_DEBOUNCE_DURATION)
|
||||||
|
{
|
||||||
if let Err(error) = watcher_notify.send(()) {
|
if let Err(error) = watcher_notify.send(()) {
|
||||||
error!("unable to send watcher file changed event: {}", error);
|
error!("unable to send watcher file changed event: {}", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
last_event_arrival = Instant::now();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user