From f52f43fe90d49791f9aebcef19b6dc9f00e7e01c Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 3 Jan 2018 18:02:56 -0800 Subject: [PATCH] Eliminate extra thread for VfsWatcher --- src/commands/serve.rs | 10 +---- src/vfs/vfs_watcher.rs | 98 ++++++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/commands/serve.rs b/src/commands/serve.rs index 8ffc3790..c68881a0 100644 --- a/src/commands/serve.rs +++ b/src/commands/serve.rs @@ -1,6 +1,5 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; -use std::thread; use std::process; use rand; @@ -100,15 +99,8 @@ pub fn serve(project_path: &PathBuf, verbose: bool, port: Option) { Arc::new(Mutex::new(vfs)) }; - { - let vfs = vfs.clone(); - - thread::spawn(move || { - VfsWatcher::new(vfs).start(); - }); - } - println!("Server listening on port {}", web_config.port); + VfsWatcher::new(vfs.clone()).start(); web::start(web_config, project.clone(), &PLUGIN_CHAIN, vfs.clone()); } diff --git a/src/vfs/vfs_watcher.rs b/src/vfs/vfs_watcher.rs index 5a5cd91b..4598e72b 100644 --- a/src/vfs/vfs_watcher.rs +++ b/src/vfs/vfs_watcher.rs @@ -23,71 +23,65 @@ impl VfsWatcher { } pub fn start(mut self) { - { - let outer_vfs = self.vfs.lock().unwrap(); + let outer_vfs = self.vfs.lock().unwrap(); - for (partition_name, root_path) in &outer_vfs.partitions { - let (tx, rx) = mpsc::channel(); - let partition_name = partition_name.clone(); - let root_path = root_path.clone(); + for (partition_name, root_path) in &outer_vfs.partitions { + let (tx, rx) = mpsc::channel(); + let partition_name = partition_name.clone(); + let root_path = root_path.clone(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(1)) - .expect("Unable to create watcher!"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(1)) + .expect("Unable to create watcher!"); - watcher - .watch(&root_path, RecursiveMode::Recursive) - .expect("Unable to watch path!"); + watcher + .watch(&root_path, RecursiveMode::Recursive) + .expect("Unable to watch path!"); - self.watchers.push(watcher); + self.watchers.push(watcher); - { - let vfs = self.vfs.clone(); + { + let vfs = self.vfs.clone(); - thread::spawn(move || { - loop { - let event = rx.recv().unwrap(); - let mut vfs = vfs.lock().unwrap(); - let current_time = vfs.current_time(); + thread::spawn(move || { + loop { + let event = rx.recv().unwrap(); + let mut vfs = vfs.lock().unwrap(); + let current_time = vfs.current_time(); - match event { - DebouncedEvent::Write(ref change_path) | - DebouncedEvent::Create(ref change_path) | - DebouncedEvent::Remove(ref change_path) => { - if let Some(mut route) = path_to_route(&root_path, change_path) { - route.insert(0, partition_name.clone()); + match event { + DebouncedEvent::Write(ref change_path) | + DebouncedEvent::Create(ref change_path) | + DebouncedEvent::Remove(ref change_path) => { + if let Some(mut route) = path_to_route(&root_path, change_path) { + route.insert(0, partition_name.clone()); - vfs.add_change(current_time, route); - } else { - println!("Failed to get route from {}", change_path.display()); - } - }, - DebouncedEvent::Rename(ref from_change, ref to_change) => { - if let Some(mut route) = path_to_route(&root_path, from_change) { - route.insert(0, partition_name.clone()); + vfs.add_change(current_time, route); + } else { + println!("Failed to get route from {}", change_path.display()); + } + }, + DebouncedEvent::Rename(ref from_change, ref to_change) => { + if let Some(mut route) = path_to_route(&root_path, from_change) { + route.insert(0, partition_name.clone()); - vfs.add_change(current_time, route); - } else { - println!("Failed to get route from {}", from_change.display()); - } + vfs.add_change(current_time, route); + } else { + println!("Failed to get route from {}", from_change.display()); + } - if let Some(mut route) = path_to_route(&root_path, to_change) { - route.insert(0, partition_name.clone()); + if let Some(mut route) = path_to_route(&root_path, to_change) { + route.insert(0, partition_name.clone()); - vfs.add_change(current_time, route); - } else { - println!("Failed to get route from {}", to_change.display()); - } - }, - _ => {}, - } + vfs.add_change(current_time, route); + } else { + println!("Failed to get route from {}", to_change.display()); + } + }, + _ => {}, } - }); - } + } + }); } } - - loop { - thread::park(); - } } }