DEV Community

WH yang
WH yang

Posted on

Enhance Deno.watchFs

Deno has a built-in API called watchFs that tracks changes in files and folders. It can also watch subfolders automatically. However, some files, like those in the .git folder, change a lot. It would be helpful to have an "ignore" option that skips events from .git and its subfolders. I found this issue suggesting this idea.

I created a pull request for this new ignore option. This update changes fs_events.rs, which is the Rust code behind the Deno.watchFs API. My update checks if an event belongs to any ignored paths. If it does, it won’t send that event back to the JavaScript layer.

  let ignore = ignore.iter().any(|path| {
            event.paths.iter().any(|event_path| {
              same_file::is_same_file(event_path, path).unwrap_or(false)
                || starts_with_canonicalized(event_path, path)
            })
          });
 if !ignore && paths.iter().any(|path| { ... }
Enter fullscreen mode Exit fullscreen mode

The original proposal also mentioned that watching a folder with many nested subfolders can be slow. I tried a different method that manually walk each folder and creates watchers but skips ignored paths. However, this method was slower than the simple filter I implemented. Managing multiple file watchers is less efficient than one that watches everything at once for OS. Also, new folders won't be detected if they weren't included in the initial walk.

Top comments (0)