From 9da5a58439d939aa28c6c9a4871b05f9eb019fb1 Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 19 Feb 2025 19:14:06 +0800 Subject: [PATCH 1/5] feat: support wasm build --- notify-debouncer-full/src/cache.rs | 106 +++++++++++++++-------------- notify-debouncer-full/src/lib.rs | 2 +- 2 files changed, 57 insertions(+), 51 deletions(-) diff --git a/notify-debouncer-full/src/cache.rs b/notify-debouncer-full/src/cache.rs index b0d0b989..06f9fd57 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,67 @@ 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() +mod file_id_map { + 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, } - fn dir_scan_depth(is_recursive: bool) -> usize { - if is_recursive { - usize::MAX - } else { - 1 + impl FileIdMap { + /// Construct an empty cache. + pub fn new() -> Self { + Default::default() } - } -} -impl FileIdCache for FileIdMap { - fn cached_file_id(&self, path: &Path) -> Option> { - self.paths.get(path) + fn dir_scan_depth(is_recursive: bool) -> usize { + if is_recursive { + usize::MAX + } else { + 1 + } + } } - 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); + impl FileIdCache for FileIdMap { + fn cached_file_id(&self, path: &Path) -> Option> { + self.paths.get(path) } - } - fn remove_path(&mut self, path: &Path) { - self.paths.retain(|p, _| !p.starts_with(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 +119,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 = file_id_map::FileIdMap; diff --git a/notify-debouncer-full/src/lib.rs b/notify-debouncer-full/src/lib.rs index b45c640f..a77d21be 100644 --- a/notify-debouncer-full/src/lib.rs +++ b/notify-debouncer-full/src/lib.rs @@ -79,7 +79,7 @@ use std::{ use time::now; -pub use cache::{FileIdCache, FileIdMap, NoCache, RecommendedCache}; +pub use cache::{FileIdCache, NoCache, RecommendedCache}; pub use file_id; pub use notify; From 28d6519e0547e216bf5b4c84d794d12172fa6338 Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 19 Feb 2025 19:41:02 +0800 Subject: [PATCH 2/5] feat: add missing changes --- notify-debouncer-full/src/cache.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/notify-debouncer-full/src/cache.rs b/notify-debouncer-full/src/cache.rs index 06f9fd57..e23adf88 100644 --- a/notify-debouncer-full/src/cache.rs +++ b/notify-debouncer-full/src/cache.rs @@ -34,6 +34,7 @@ pub trait FileIdCache { } } +#[cfg(not(any(target_os = "linux", target_os = "android", target_family = "wasm")))] mod file_id_map { use crate::FileIdCache; use file_id::{get_file_id, FileId}; From b5cec17b6e0a27db8980159e09a60f618f490665 Mon Sep 17 00:00:00 2001 From: underfin <2218301630@qq.com> Date: Thu, 20 Feb 2025 11:21:44 +0800 Subject: [PATCH 3/5] Update notify-debouncer-full/src/lib.rs Co-authored-by: Daniel Faust --- notify-debouncer-full/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/notify-debouncer-full/src/lib.rs b/notify-debouncer-full/src/lib.rs index a77d21be..9fff7648 100644 --- a/notify-debouncer-full/src/lib.rs +++ b/notify-debouncer-full/src/lib.rs @@ -81,6 +81,9 @@ use time::now; pub use cache::{FileIdCache, NoCache, RecommendedCache}; +#[cfg(not(target_family = "wasm"))] +pub use file_id_map::FileIdMap; + pub use file_id; pub use notify; pub use notify_types::debouncer_full::DebouncedEvent; From 19d8ebdaa55f978795806e091a5e4b9c9fbd03b8 Mon Sep 17 00:00:00 2001 From: underfin <2218301630@qq.com> Date: Thu, 20 Feb 2025 11:23:24 +0800 Subject: [PATCH 4/5] Update notify-debouncer-full/src/cache.rs Co-authored-by: Daniel Faust --- notify-debouncer-full/src/cache.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notify-debouncer-full/src/cache.rs b/notify-debouncer-full/src/cache.rs index e23adf88..26c1ef72 100644 --- a/notify-debouncer-full/src/cache.rs +++ b/notify-debouncer-full/src/cache.rs @@ -34,7 +34,7 @@ pub trait FileIdCache { } } -#[cfg(not(any(target_os = "linux", target_os = "android", target_family = "wasm")))] +#[cfg(not(target_family = "wasm"))] mod file_id_map { use crate::FileIdCache; use file_id::{get_file_id, FileId}; From 1495ed300c6b0b0ba19a7e4bef2d5b294714973a Mon Sep 17 00:00:00 2001 From: underfin Date: Thu, 20 Feb 2025 11:40:48 +0800 Subject: [PATCH 5/5] fix: add file_id_map file --- notify-debouncer-full/src/cache.rs | 64 +----------------------- notify-debouncer-full/src/file_id_map.rs | 59 ++++++++++++++++++++++ notify-debouncer-full/src/lib.rs | 3 ++ 3 files changed, 63 insertions(+), 63 deletions(-) create mode 100644 notify-debouncer-full/src/file_id_map.rs diff --git a/notify-debouncer-full/src/cache.rs b/notify-debouncer-full/src/cache.rs index 26c1ef72..b894b74d 100644 --- a/notify-debouncer-full/src/cache.rs +++ b/notify-debouncer-full/src/cache.rs @@ -34,68 +34,6 @@ pub trait FileIdCache { } } -#[cfg(not(target_family = "wasm"))] -mod file_id_map { - 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)); - } - } -} /// 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. @@ -124,4 +62,4 @@ impl FileIdCache for NoCache { pub type RecommendedCache = NoCache; /// The recommended file ID cache implementation for the current platform #[cfg(not(any(target_os = "linux", target_os = "android", target_family = "wasm")))] -pub type RecommendedCache = file_id_map::FileIdMap; +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 9fff7648..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},