use std::sync::{Arc, PoisonError, RwLock};
use orbit_orm::Database;
use serde::Serialize;
use crate::RuntimeFuture;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MediaItem {
pub id: i64,
pub title: String,
pub url: String,
}
pub trait MediaLibrary: Send + Sync + 'static {
fn find<'a>(&'a self, db: &'a Database, ids: &'a [i64]) -> RuntimeFuture<'a, Vec<MediaItem>>;
fn browse_url(&self) -> Option<String> {
None
}
}
static LIBRARY: RwLock<Option<Arc<dyn MediaLibrary>>> = RwLock::new(None);
pub fn register_media_library(library: impl MediaLibrary) {
let mut slot = LIBRARY.write().unwrap_or_else(PoisonError::into_inner);
*slot = Some(Arc::new(library));
}
pub fn media_library() -> Option<Arc<dyn MediaLibrary>> {
LIBRARY
.read()
.unwrap_or_else(PoisonError::into_inner)
.clone()
}