Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider supporting fileTreeVisitor option in DirectorWatcher #7

Open
jeroenvandijk opened this issue Nov 15, 2023 · 1 comment
Open

Comments

@jeroenvandijk
Copy link
Contributor

Story

I have a scenario where I'm watching a directory for one specific file (to be added/changed/deleted). However this file is in a directory with many subdirectories and nested files. Calling beholder/watch on this dir takes about 1500 milliseconds on my machine. I did some experimentation by adding a custom visitor to the DirectorWatcher and I was able to bring this down to 5 milliseconds. Maybe this is interesting to add to beholder as an option.

Possible implementation

The DirectoryWatcher.Builder class allows to add a custom visitor option. This is currently not supported by beholder, but it could be added quite simple (see below). However the create would have to be made public or it would have to be forwarded via one of the watch functions, so API design is the hardest part here.

Below would be a possible implementation. Here is also a gist of how to create a visitor that can be used here.

(defn create
  "Creates a watcher taking a callback function `cb` that will be invoked
  whenever a file in one of the `paths` chages.

  Not meant to be called directly but use `watch` or `watch-blocking` instead."
  [cb paths & {:keys [^FileTreeVisitor visitor]}]
  (-> (cond-> (DirectoryWatcher/builder)
        visitor
        (.fileTreeVisitor visitor))
      (.paths (map to-path paths))
      (.listener (fn->listener cb))
      (.build)))
@jeroenvandijk
Copy link
Contributor Author

FYI, I had expected that adding a visitor with :max-depth 1 would not give notifications for nested directories, but that doesn't seem to be the case. However it does load much faster for directories with many subdirectories. So I'm not sure what's happening behind the scenes exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant