use futures_util::future::BoxFuture;
use tokio::sync::mpsc::{self, Receiver};
use crate::{
ErrorKind, Result,
watch::{Event, StorageMonitor},
};
use super::VdirStorage;
pub struct VdirMonitor {
receiver: Receiver<Event>,
}
impl VdirMonitor {
#[allow(unused)] #[allow(clippy::unused_async)] pub async fn new(storage: &VdirStorage) -> Result<VdirMonitor> {
let (sender, receiver) = mpsc::channel::<Event>(1);
#[cfg(target_os = "linux")]
{
use crate::vdir::linux::{init_inotify, run_inotify_task};
let path = storage.path.clone();
let extension = storage.extension.clone();
let inotify_watcher = init_inotify(&path).await?;
tokio::spawn(async move {
if sender.send(Event::General).await.is_err() {
return;
}
run_inotify_task(sender, path, extension, inotify_watcher).await;
});
return Ok(VdirMonitor { receiver });
}
#[allow(unreachable_code)]
Err(ErrorKind::Unsupported.into())
}
}
impl StorageMonitor for VdirMonitor {
fn next_event(&mut self) -> BoxFuture<'_, Option<Event>> {
Box::pin(async { self.receiver.recv().await })
}
}