All tests run on an 8-year-old MacBook Air.
Drop a PDF into a folder. By the time you look at your screen, it's already been processed.
That's the Hot Folder feature — and it's powered by notify, a cross-platform file system watcher for Rust.
Basic setup
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::Path;
use std::sync::mpsc;
pub fn watch_folder(
folder: &Path,
tx: mpsc::Sender>,
) -> notify::Result {
let mut watcher = RecommendedWatcher::new(
move |res| { tx.send(res).unwrap(); },
Config::default(),
)?;
watcher.watch(folder, RecursiveMode::NonRecursive)?;
Ok(watcher)
}
RecommendedWatcher uses FSEvents on macOS — low overhead, near-instant detection.
Handling events
Not every event should trigger the pipeline. Filter for file creation only, and only for PDFs:
pub fn handle_events(rx: mpsc::Receiver>) {
for res in rx {
match res {
Ok(event) => {
if event.kind.is_create() {
for path in event.paths {
if path.extension().map_or(false, |e| e == "pdf") {
tokio::spawn(run_pipeline(path));
}
}
}
}
Err(e) => eprintln!("watch error: {e}"),
}
}
}
The debounce problem
File creation events fire multiple times for a single file — once when the file is created, again as bytes are written. Processing a half-written PDF will fail.
Fix: wait for the Modify events to stop before processing.
use notify::Config;
use std::time::Duration;
// Built-in debounce via notify-debouncer-mini
use notify_debouncer_mini::{new_debouncer, DebounceEventResult};
let (tx, rx) = mpsc::channel::();
let mut debouncer = new_debouncer(
Duration::from_millis(500), // wait 500ms after last event
move |res| { tx.send(res).unwrap(); },
)?;
500ms after the last write event — the file is complete. Process it then.
Wiring into Tauri
The watcher runs in a background thread. When the user adds or removes a watched folder from the UI, send a message to that thread:
pub enum WatcherCommand {
Add(PathBuf),
Remove(PathBuf),
Shutdown,
}
Keep the watcher alive by storing it in Tauri's managed state. Drop it on app exit.
Hiyoko PDF Vault → https://hiyokoko.gumroad.com/l/HiyokoPDFVault
X → @hiyoyok
Top comments (0)