use std::path::Path;
use std::sync::Arc;
pub trait ProgressSink: Send + Sync {
fn on_file(&self, path: &Path, bytes: u64);
}
impl<F> ProgressSink for F
where
F: Fn(&Path, u64) + Send + Sync,
{
fn on_file(&self, path: &Path, bytes: u64) {
self(path, bytes)
}
}
static SINK: std::sync::RwLock<Option<Arc<dyn ProgressSink>>> = std::sync::RwLock::new(None);
pub fn set_sink(sink: Arc<dyn ProgressSink>) {
*SINK.write().expect("progress sink lock poisoned") = Some(sink);
}
pub fn clear_sink() {
*SINK.write().expect("progress sink lock poisoned") = None;
}
pub fn emit_file(path: &Path, bytes: u64) {
let sink = SINK.read().expect("progress sink lock poisoned").clone();
if let Some(sink) = sink {
sink.on_file(path, bytes);
}
}