From a6fb3971c6640cb0e331ff84c5ee2fd03879bfe3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 27 Feb 2020 22:33:16 -0800 Subject: [PATCH] fix an issue where config was not reloaded when changed Weirdly, this seemed specific to lua files rather than toml files, which I don't have a good explanation for. When saving the file in vim, we'd get a NoticeRemove but no Remove event. This commit synthesizes the equivalent of a Remove after a NoticeRemove and a slight delay. --- src/config/mod.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 3177044da..f23073457 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -93,7 +93,8 @@ impl ConfigInner { fn watch_path(&mut self, path: PathBuf) { if self.watcher.is_none() { let (tx, rx) = std::sync::mpsc::channel(); - let watcher = notify::watcher(tx, Duration::from_millis(200)).unwrap(); + const DELAY: Duration = Duration::from_millis(200); + let watcher = notify::watcher(tx, DELAY).unwrap(); std::thread::spawn(move || { // block until we get an event use notify::DebouncedEvent; @@ -103,17 +104,19 @@ impl ConfigInner { // Defer acting until `Write`, otherwise we'll // reload twice in quick succession DebouncedEvent::NoticeWrite(_) => None, - // Likewise, defer processing a remove until after - // we've debounced the event. That will give us - // time to pick up the new version of the config if - // the user's editor removes the file before writing - // out a new version. - DebouncedEvent::NoticeRemove(_) => None, DebouncedEvent::Create(path) | DebouncedEvent::Write(path) | DebouncedEvent::Chmod(path) | DebouncedEvent::Remove(path) | DebouncedEvent::Rename(path, _) => Some(path), + DebouncedEvent::NoticeRemove(path) => { + // In theory, `notify` should deliver DebouncedEvent::Remove + // shortly after this, but it doesn't always do so. + // Let's just wait a bit and report the path changed + // for ourselves. + std::thread::sleep(DELAY); + Some(path) + } DebouncedEvent::Error(_, path) => path, DebouncedEvent::Rescan => None, }