-
Notifications
You must be signed in to change notification settings - Fork 230
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
[Feature request] Make results of initial scan available #478
Comments
There is no initial scan on windows. So this would be a special api for inotify / pollwatcher backend. |
Ah, I tricked myself the notify is doing initial scan as I only used pollwatcher. Okay, it seems legit now |
This would be useful, I already use walkdir prior to notify (pollwatcher), this would prevent double scanning, speeding up my program startup, and reduce dependencies in my project. |
How exactly would you like to access the initial scan ? Or do you want to access the live state too ? One could want access to the full internal path cache, or alternatively provide a callback function that gets called for each path found during the initial scan. |
I suppose whichever is easier, pollwatcher uses walkdir internally so exposing that would probably be easiest, but I'm not sure Maybe an option to emit the first scan results to the existing event callback? That might be more work |
If we can change emitted event kinds, I believe the best way would be to add event callback kind for |
Is there a way to optionally supply the same handler for both? let mut watcher = PollWatcher::with_initial_scan(tx, notify_config, Some(tx))?; EDIT: It seems the ScanEventHandler sends a ScanEvent which is a type alias for Result but the type alias for EventHandler excludes the Result, instead Result struct Handler;
impl EventHandler for Handler {
fn handle_event(&mut self, event: notify::Result<Event>) {
todo!()
}
}
impl ScanEventHandler for Handler {
fn handle_event(&mut self, event: notify::Result<PathBuf>) {
todo!()
}
} |
Hm yeah, I opted for putting the Result inside the ScanEvent type - maybe that's more confusing than anything (changing this for notify would be a semver break). |
I left it like that and added some info to the ScanEventHandler docs. |
Returning a result is fine, I just meant that normal event is wrapped in a result, while a scan event includes the result not just the pathbuf Whats the purpose of the option parameter since the with variant is optional to use? |
Yeah changing this to use the result in the normal event would be a major version change. But for the scan events it definitely makes sense to have this as one type. |
Hm I could internalize this so you don't have to add a |
Yeah, since we already include I looked at the example and it seems to be exactly what I needed. I will check the implementation in actual app in a few moments |
This is what I came up with but use std::{
path::{Path, PathBuf},
time::Duration,
};
use notify::{Config, Event, PollWatcher, RecursiveMode, Watcher};
fn main() {
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = PollWatcher::with_initial_scan(
|event_result: notify::Result<Event>| {
let Ok(event) = event_result else {
return;
};
for path in event.paths {
tx.send(path).unwrap();
}
},
Config::default().with_poll_interval(Duration::from_secs(1)),
Some(|path_result: notify::Result<PathBuf>| {
let Ok(path) = path_result else {
return;
};
tx.send(path).unwrap();
}),
)
.unwrap();
watcher
.watch(Path::new("."), RecursiveMode::Recursive)
.unwrap();
loop {
let path = rx.recv().unwrap();
println!("path: {:?}", path);
}
} |
fn main() {
let (tx, rx) = std::sync::mpsc::channel();
let tx_c = tx.clone();
let mut watcher = PollWatcher::with_initial_scan(
move |event_result: notify::Result<Event>| {
let Ok(event) = event_result else {
return;
};
for path in event.paths {
tx_c.send(path).unwrap();
}
},
Config::default().with_poll_interval(Duration::from_secs(1)),
move |path_result: notify::Result<PathBuf>| {
let Ok(path) = path_result else {
return;
};
tx.send(path).unwrap();
},
)
.unwrap();
//[..]
} See also the example. You need to |
👍 It works for me, I can remove walkdir, rayon, and a few functions from my project now |
Looks good indeed, thank you! |
Is there a planned release soon or can a release be made with these changes? I want to publish to crates.io but I can't use git dependencies on crates.io |
I have a release planned, there are just some PRs I want to merge first |
This is usecase for #339
Scenario:
Now I cannot obtain initial list of files from the API. I have to call separately
walkdir
which imply some drawbacks:walkdir
output (unless notify is usingwalkdir
internally, I haven't check up that)The text was updated successfully, but these errors were encountered: