use std::io;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use crate::monitor::{Core, Request};
use crate::queue::{Sender, WatchId};
use crate::servicing::Rejected;
use crate::watch::{VolumeChangeDecision, Watch, WatchOptions};
#[derive(Clone)]
pub struct Session {
core: Arc<Core>,
sink: Sender,
}
impl Session {
pub(crate) fn new(core: Arc<Core>, sink: Sender) -> Self {
Self { core, sink }
}
pub fn subscribe(&self, path: impl AsRef<Path>, options: WatchOptions) -> io::Result<Watch> {
crate::watch::subscribe(self, path.as_ref(), options)
}
pub fn answer(&self, watch: WatchId, delay: Option<Duration>) {
let _ = self.submit(Request::Answer { watch, delay });
}
pub fn answer_volume_change(&self, watch: WatchId, decision: VolumeChangeDecision) {
let _ = self.submit(Request::AnswerVolumeChange { watch, decision });
}
pub(crate) fn submit(&self, request: Request) -> Result<(), Rejected<Request>> {
self.core.submit(request)
}
#[must_use]
pub fn is_open(&self) -> bool {
self.core.is_open()
}
pub(crate) fn next_watch(&self) -> WatchId {
self.core.next_watch()
}
pub(crate) fn sink(&self) -> &Sender {
&self.sink
}
}
impl std::fmt::Debug for Session {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Session")
.field("open", &self.is_open())
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests;