hide TaskResult from the public API

This commit is contained in:
Aleksey Kladov 2019-02-18 14:29:54 +03:00
parent 062aa97235
commit c5a65466e2
2 changed files with 28 additions and 15 deletions

View file

@ -9,7 +9,7 @@ use relative_path::RelativePathBuf;
use walkdir::WalkDir; use walkdir::WalkDir;
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher as _Watcher}; use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher as _Watcher};
use crate::{Roots, VfsRoot}; use crate::{Roots, VfsRoot, VfsTask};
pub(crate) enum Task { pub(crate) enum Task {
AddRoot { root: VfsRoot }, AddRoot { root: VfsRoot },
@ -18,7 +18,7 @@ pub(crate) enum Task {
/// `TaskResult` transfers files read on the IO thread to the VFS on the main /// `TaskResult` transfers files read on the IO thread to the VFS on the main
/// thread. /// thread.
#[derive(Debug)] #[derive(Debug)]
pub enum TaskResult { pub(crate) enum TaskResult {
/// Emitted when we've recursively scanned a source root during the initial /// Emitted when we've recursively scanned a source root during the initial
/// load. /// load.
BulkLoadRoot { root: VfsRoot, files: Vec<(RelativePathBuf, String)> }, BulkLoadRoot { root: VfsRoot, files: Vec<(RelativePathBuf, String)> },
@ -46,7 +46,7 @@ enum ChangeKind {
const WATCHER_DELAY: Duration = Duration::from_millis(250); const WATCHER_DELAY: Duration = Duration::from_millis(250);
pub(crate) type Worker = thread_worker::Worker<Task, TaskResult>; pub(crate) type Worker = thread_worker::Worker<Task, VfsTask>;
pub(crate) fn start(roots: Arc<Roots>) -> Worker { pub(crate) fn start(roots: Arc<Roots>) -> Worker {
// This is a pretty elaborate setup of threads & channels! It is // This is a pretty elaborate setup of threads & channels! It is
// explained by the following concerns: // explained by the following concerns:
@ -122,7 +122,7 @@ pub(crate) fn start(roots: Arc<Roots>) -> Worker {
fn watch_root( fn watch_root(
watcher: Option<&mut RecommendedWatcher>, watcher: Option<&mut RecommendedWatcher>,
sender: &Sender<TaskResult>, sender: &Sender<VfsTask>,
roots: &Roots, roots: &Roots,
root: VfsRoot, root: VfsRoot,
) { ) {
@ -136,7 +136,8 @@ fn watch_root(
Some((path, text)) Some((path, text))
}) })
.collect(); .collect();
sender.send(TaskResult::BulkLoadRoot { root, files }).unwrap(); let res = TaskResult::BulkLoadRoot { root, files };
sender.send(VfsTask(res)).unwrap();
log::debug!("... loaded {}", root_path.display()); log::debug!("... loaded {}", root_path.display());
} }
@ -173,7 +174,7 @@ fn convert_notify_event(event: DebouncedEvent, sender: &Sender<(PathBuf, ChangeK
fn handle_change( fn handle_change(
watcher: Option<&mut RecommendedWatcher>, watcher: Option<&mut RecommendedWatcher>,
sender: &Sender<TaskResult>, sender: &Sender<VfsTask>,
roots: &Roots, roots: &Roots,
path: PathBuf, path: PathBuf,
kind: ChangeKind, kind: ChangeKind,
@ -195,13 +196,15 @@ fn handle_change(
.try_for_each(|rel_path| { .try_for_each(|rel_path| {
let abs_path = rel_path.to_path(&roots.path(root)); let abs_path = rel_path.to_path(&roots.path(root));
let text = read_to_string(&abs_path); let text = read_to_string(&abs_path);
sender.send(TaskResult::SingleFile { root, path: rel_path, text }) let res = TaskResult::SingleFile { root, path: rel_path, text };
sender.send(VfsTask(res))
}) })
.unwrap() .unwrap()
} }
ChangeKind::Write | ChangeKind::Remove => { ChangeKind::Write | ChangeKind::Remove => {
let text = read_to_string(&path); let text = read_to_string(&path);
sender.send(TaskResult::SingleFile { root, path: rel_path, text }).unwrap(); let res = TaskResult::SingleFile { root, path: rel_path, text };
sender.send(VfsTask(res)).unwrap();
} }
} }
} }

View file

@ -33,10 +33,20 @@ use crate::{
roots::Roots, roots::Roots,
}; };
pub use crate::{ pub use crate::roots::VfsRoot;
io::TaskResult as VfsTask,
roots::VfsRoot, /// Opaque wrapper around file-system event.
}; ///
/// Calling code is expected to just pass `VfsTask` to `handle_task` method. It
/// is exposed as a public API so that the caller can plug vfs events into the
/// main event loop and be notified when changes happen.
pub struct VfsTask(TaskResult);
impl fmt::Debug for VfsTask {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("VfsTask { ... }")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VfsFile(pub u32); pub struct VfsFile(pub u32);
@ -159,12 +169,12 @@ impl Vfs {
mem::replace(&mut self.pending_changes, Vec::new()) mem::replace(&mut self.pending_changes, Vec::new())
} }
pub fn task_receiver(&self) -> &Receiver<io::TaskResult> { pub fn task_receiver(&self) -> &Receiver<VfsTask> {
self.worker.receiver() self.worker.receiver()
} }
pub fn handle_task(&mut self, task: io::TaskResult) { pub fn handle_task(&mut self, task: VfsTask) {
match task { match task.0 {
TaskResult::BulkLoadRoot { root, files } => { TaskResult::BulkLoadRoot { root, files } => {
let mut cur_files = Vec::new(); let mut cur_files = Vec::new();
// While we were scanning the root in the background, a file might have // While we were scanning the root in the background, a file might have