use std::path::Path;
use notify::RecursiveMode;
use notify_debouncer_mini::{new_debouncer, DebounceEventResult, Debouncer};
use crate::{rebuild_and_reload, AppState};
pub fn spawn_watcher(
state: AppState,
docs_dir: &Path,
) -> anyhow::Result<Debouncer<notify::RecommendedWatcher>> {
let mut debouncer = new_debouncer(
std::time::Duration::from_millis(200),
move |res: DebounceEventResult| match res {
Ok(_events) => {
if state.take_self_write_suppression() {
tracing::debug!("skipping watcher rebuild: editor-initiated write");
return;
}
if let Err(e) = rebuild_and_reload(&state) {
tracing::error!("rebuild after fs change failed: {e:#}");
}
}
Err(e) => tracing::error!("watch error: {e:?}"),
},
)?;
debouncer
.watcher()
.watch(docs_dir, RecursiveMode::Recursive)?;
Ok(debouncer)
}