use crate::api::ContextApi;
use crate::error::ApiError;
use crate::types::NodeID;
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::PathBuf;
use std::sync::mpsc;
use tracing::error;
pub struct EditorHooks {
#[allow(dead_code)]
api: ContextApi,
workspace_root: PathBuf,
}
impl EditorHooks {
pub fn new(api: ContextApi, workspace_root: PathBuf) -> Self {
Self {
api,
workspace_root,
}
}
pub fn watch(
&self,
) -> Result<(mpsc::Receiver<notify::Result<Event>>, RecommendedWatcher), ApiError> {
let (tx, rx) = mpsc::channel();
let mut watcher = notify::recommended_watcher(move |res| {
if let Err(e) = tx.send(res) {
error!("Error sending watch event: {}", e);
}
})
.map_err(|e| {
ApiError::StorageError(crate::error::StorageError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to create watcher: {}", e),
)))
})?;
watcher
.watch(&self.workspace_root, RecursiveMode::Recursive)
.map_err(|e| {
ApiError::StorageError(crate::error::StorageError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to watch directory: {}", e),
)))
})?;
Ok((rx, watcher))
}
pub fn handle_event(&self, event: Event) -> Result<Option<NodeID>, ApiError> {
match event.kind {
EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_) => Ok(None),
_ => Ok(None),
}
}
pub fn on_node_change<F>(&self, _callback: F)
where
F: Fn(NodeID) + Send + Sync + 'static,
{
}
}