From 23258d8436ab8cfda74a3ac5dc1a30ec63bfb6e6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 6 Jul 2020 09:28:17 +0200 Subject: Simplify file watcher --- crates/vfs-notify/src/lib.rs | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/crates/vfs-notify/src/lib.rs b/crates/vfs-notify/src/lib.rs index b1ea298ae..ee5035554 100644 --- a/crates/vfs-notify/src/lib.rs +++ b/crates/vfs-notify/src/lib.rs @@ -10,10 +10,9 @@ mod include; use std::convert::{TryFrom, TryInto}; -use crossbeam_channel::{select, unbounded, Receiver, Sender}; +use crossbeam_channel::{never, select, unbounded, Receiver, Sender}; use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use paths::{AbsPath, AbsPathBuf}; -use rustc_hash::FxHashSet; use vfs::loader; use walkdir::WalkDir; @@ -55,10 +54,8 @@ type NotifyEvent = notify::Result; struct NotifyActor { sender: loader::Sender, config: Vec<(AbsPathBuf, Include, bool)>, - watched_paths: FxHashSet, - // Drop order of fields bellow is significant, - watcher: Option, - watcher_receiver: Receiver, + // Drop order is significant. + watcher: Option<(RecommendedWatcher, Receiver)>, } #[derive(Debug)] @@ -69,23 +66,13 @@ enum Event { impl NotifyActor { fn new(sender: loader::Sender) -> NotifyActor { - let (watcher_sender, watcher_receiver) = unbounded(); - let watcher = log_notify_error(Watcher::new_immediate(move |event| { - watcher_sender.send(event).unwrap() - })); - - NotifyActor { - sender, - config: Vec::new(), - watched_paths: FxHashSet::default(), - watcher, - watcher_receiver, - } + NotifyActor { sender, config: Vec::new(), watcher: None } } fn next_event(&self, receiver: &Receiver) -> Option { + let watcher_receiver = self.watcher.as_ref().map(|(_, receiver)| receiver); select! { recv(receiver) -> it => it.ok().map(Event::Message), - recv(&self.watcher_receiver) -> it => Some(Event::NotifyEvent(it.unwrap())), + recv(watcher_receiver.unwrap_or(&never())) -> it => Some(Event::NotifyEvent(it.unwrap())), } } fn run(mut self, inbox: Receiver) { @@ -94,10 +81,16 @@ impl NotifyActor { match event { Event::Message(msg) => match msg { Message::Config(config) => { + self.watcher = None; + let (watcher_sender, watcher_receiver) = unbounded(); + let watcher = log_notify_error(Watcher::new_immediate(move |event| { + watcher_sender.send(event).unwrap() + })); + self.watcher = watcher.map(|it| (it, watcher_receiver)); + let n_total = config.load.len(); self.send(loader::Message::Progress { n_total, n_done: 0 }); - self.unwatch_all(); self.config.clear(); for (i, entry) in config.load.into_iter().enumerate() { @@ -217,16 +210,8 @@ impl NotifyActor { } fn watch(&mut self, path: AbsPathBuf) { - if let Some(watcher) = &mut self.watcher { + if let Some((watcher, _)) = &mut self.watcher { log_notify_error(watcher.watch(&path, RecursiveMode::NonRecursive)); - self.watched_paths.insert(path); - } - } - fn unwatch_all(&mut self) { - if let Some(watcher) = &mut self.watcher { - for path in self.watched_paths.drain() { - log_notify_error(watcher.unwatch(path)); - } } } fn send(&mut self, msg: loader::Message) { -- cgit v1.2.3