use std::{collections::HashMap, path::PathBuf, sync::Arc};
use tempfile::TempDir;
use tokio::sync::{Mutex, RwLock};
use void_crawl_core::{BrowserSession, DownloadCapture, Page};
pub type SessionId = String;
#[derive(Debug)]
pub struct PendingDownload {
pub capture: DownloadCapture,
pub quarantine: TempDir,
pub output_dir: PathBuf,
pub max_bytes: u64,
}
#[derive(Debug)]
pub struct DedicatedSession {
pub session: Arc<BrowserSession>,
pub page: Mutex<Page>,
pub pending_download: Mutex<Option<PendingDownload>>,
}
#[derive(Debug, Default)]
pub struct SessionRegistry {
inner: RwLock<HashMap<SessionId, Arc<DedicatedSession>>>,
}
impl SessionRegistry {
pub async fn insert(&self, id: SessionId, session: Arc<DedicatedSession>) {
self.inner.write().await.insert(id, session);
}
pub async fn get(&self, id: &str) -> Option<Arc<DedicatedSession>> {
self.inner.read().await.get(id).cloned()
}
pub async fn remove(&self, id: &str) -> Option<Arc<DedicatedSession>> {
self.inner.write().await.remove(id)
}
pub async fn len(&self) -> usize {
self.inner.read().await.len()
}
pub async fn is_empty(&self) -> bool {
self.inner.read().await.is_empty()
}
pub async fn drain(&self) -> Vec<Arc<DedicatedSession>> {
self.inner.write().await.drain().map(|(_, v)| v).collect()
}
}