diff --git a/notify-debouncer-full/src/cache.rs b/notify-debouncer-full/src/cache.rs index b0d0b989..b894b74d 100644 --- a/notify-debouncer-full/src/cache.rs +++ b/notify-debouncer-full/src/cache.rs @@ -1,10 +1,6 @@ -use std::{ - collections::HashMap, - path::{Path, PathBuf}, -}; -use file_id::{get_file_id, FileId}; +use file_id::FileId; use notify::RecursiveMode; -use walkdir::WalkDir; +use std::path::{Path, PathBuf}; /// The interface of a file ID cache. /// @@ -38,57 +34,6 @@ pub trait FileIdCache { } } -/// A cache to hold the file system IDs of all watched files. -/// -/// The file ID cache uses unique file IDs provided by the file system and is used to stich together -/// rename events in case the notification back-end doesn't emit rename cookies. -#[derive(Debug, Clone, Default)] -pub struct FileIdMap { - paths: HashMap, -} - -impl FileIdMap { - /// Construct an empty cache. - pub fn new() -> Self { - Default::default() - } - - fn dir_scan_depth(is_recursive: bool) -> usize { - if is_recursive { - usize::MAX - } else { - 1 - } - } -} - -impl FileIdCache for FileIdMap { - fn cached_file_id(&self, path: &Path) -> Option> { - self.paths.get(path) - } - - fn add_path(&mut self, path: &Path, recursive_mode: RecursiveMode) { - let is_recursive = recursive_mode == RecursiveMode::Recursive; - - for (path, file_id) in WalkDir::new(path) - .follow_links(true) - .max_depth(Self::dir_scan_depth(is_recursive)) - .into_iter() - .filter_map(|entry| { - let path = entry.ok()?.into_path(); - let file_id = get_file_id(&path).ok()?; - Some((path, file_id)) - }) - { - self.paths.insert(path, file_id); - } - } - - fn remove_path(&mut self, path: &Path) { - self.paths.retain(|p, _| !p.starts_with(path)); - } -} - /// An implementation of the `FileIdCache` trait that doesn't hold any data. /// /// This pseudo cache can be used to disable the file tracking using file system IDs. @@ -113,8 +58,8 @@ impl FileIdCache for NoCache { } /// The recommended file ID cache implementation for the current platform -#[cfg(any(target_os = "linux", target_os = "android"))] +#[cfg(any(target_os = "linux", target_os = "android", target_family = "wasm"))] pub type RecommendedCache = NoCache; /// The recommended file ID cache implementation for the current platform -#[cfg(not(any(target_os = "linux", target_os = "android")))] -pub type RecommendedCache = FileIdMap; +#[cfg(not(any(target_os = "linux", target_os = "android", target_family = "wasm")))] +pub type RecommendedCache = crate::file_id_map::FileIdMap; diff --git a/notify-debouncer-full/src/file_id_map.rs b/notify-debouncer-full/src/file_id_map.rs new file mode 100644 index 00000000..d6fe1fa1 --- /dev/null +++ b/notify-debouncer-full/src/file_id_map.rs @@ -0,0 +1,59 @@ +use crate::FileIdCache; +use file_id::{get_file_id, FileId}; +use notify::RecursiveMode; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, +}; +use walkdir::WalkDir; + +/// A cache to hold the file system IDs of all watched files. +/// +/// The file ID cache uses unique file IDs provided by the file system and is used to stich together +/// rename events in case the notification back-end doesn't emit rename cookies. +#[derive(Debug, Clone, Default)] +pub struct FileIdMap { + paths: HashMap, +} + +impl FileIdMap { + /// Construct an empty cache. + pub fn new() -> Self { + Default::default() + } + + fn dir_scan_depth(is_recursive: bool) -> usize { + if is_recursive { + usize::MAX + } else { + 1 + } + } +} + +impl FileIdCache for FileIdMap { + fn cached_file_id(&self, path: &Path) -> Option> { + self.paths.get(path) + } + + fn add_path(&mut self, path: &Path, recursive_mode: RecursiveMode) { + let is_recursive = recursive_mode == RecursiveMode::Recursive; + + for (path, file_id) in WalkDir::new(path) + .follow_links(true) + .max_depth(Self::dir_scan_depth(is_recursive)) + .into_iter() + .filter_map(|entry| { + let path = entry.ok()?.into_path(); + let file_id = get_file_id(&path).ok()?; + Some((path, file_id)) + }) + { + self.paths.insert(path, file_id); + } + } + + fn remove_path(&mut self, path: &Path) { + self.paths.retain(|p, _| !p.starts_with(path)); + } +} diff --git a/notify-debouncer-full/src/lib.rs b/notify-debouncer-full/src/lib.rs index b45c640f..8c697c9c 100644 --- a/notify-debouncer-full/src/lib.rs +++ b/notify-debouncer-full/src/lib.rs @@ -66,6 +66,9 @@ mod time; #[cfg(test)] mod testing; +#[cfg(not(target_family = "wasm"))] +mod file_id_map; + use std::{ cmp::Reverse, collections::{BinaryHeap, HashMap, VecDeque}, @@ -79,7 +82,10 @@ use std::{ use time::now; -pub use cache::{FileIdCache, FileIdMap, NoCache, RecommendedCache}; +pub use cache::{FileIdCache, NoCache, RecommendedCache}; + +#[cfg(not(target_family = "wasm"))] +pub use file_id_map::FileIdMap; pub use file_id; pub use notify;