use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_full::{DebouncedEvent, Debouncer, RecommendedCache, new_debouncer};
use serde::Deserialize;
use tauri::{
Manager, Resource, ResourceId, Runtime, Webview,
ipc::{Channel, CommandScope, GlobalScope},
path::BaseDirectory,
};
use std::time::Duration;
use crate::{
SafeFilePath,
commands::{CommandResult, resolve_path},
scope::Entry,
};
#[allow(unused)]
enum WatcherKind {
Debouncer(Debouncer<RecommendedWatcher, RecommendedCache>),
Watcher(RecommendedWatcher),
}
impl Resource for WatcherKind {}
#[derive(Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WatchOptions {
base_dir: Option<BaseDirectory>,
#[serde(default)]
recursive: bool,
delay_ms: Option<u64>,
}
#[tauri::command]
pub fn watch<R: Runtime>(
webview: Webview<R>,
paths: Vec<SafeFilePath>,
options: WatchOptions,
on_event: Channel<notify::Event>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
) -> CommandResult<ResourceId> {
let resolved_paths = paths
.into_iter()
.map(|path| {
resolve_path(
"watch",
&webview,
&global_scope,
&command_scope,
path,
options.base_dir,
)
})
.collect::<CommandResult<Vec<_>>>()?;
let recursive_mode = if options.recursive {
RecursiveMode::Recursive
} else {
RecursiveMode::NonRecursive
};
let watcher_kind = if let Some(delay) = options.delay_ms {
let mut debouncer = new_debouncer(
Duration::from_millis(delay),
None,
move |events: Result<Vec<DebouncedEvent>, Vec<notify::Error>>| match events {
Ok(events) => {
for event in events {
let _ = on_event.send(event.event);
}
}
Err(errors) => {
for error in errors {
log::error!("file watcher error: {error}");
}
}
},
)?;
for path in &resolved_paths {
debouncer.watch(path, recursive_mode)?;
}
WatcherKind::Debouncer(debouncer)
} else {
let mut watcher = RecommendedWatcher::new(
move |event: notify::Result<notify::Event>| match event {
Ok(event) => {
let _ = on_event.send(event);
}
Err(error) => log::error!("file watcher error: {error}"),
},
Config::default(),
)?;
for path in &resolved_paths {
watcher.watch(path, recursive_mode)?;
}
WatcherKind::Watcher(watcher)
};
let rid = webview.resources_table().add(watcher_kind);
Ok(rid)
}
#[cfg(test)]
mod tests {
use notify::{
Event, EventKind,
event::{DataChange, ModifyKind},
};
#[test]
fn event_kind_is_flattened_into_the_event() {
let event = Event::new(EventKind::Modify(ModifyKind::Data(DataChange::Content)))
.add_path("/tmp/file".into());
assert_eq!(
serde_json::to_value(event).unwrap(),
serde_json::json!({
"type": "modify",
"kind": "data",
"mode": "content",
"paths": ["/tmp/file"],
"attrs": {}
})
);
let event = Event::new(EventKind::Any);
assert_eq!(
serde_json::to_value(event).unwrap(),
serde_json::json!({ "type": "any", "paths": [], "attrs": {} })
);
}
}