use std::collections::HashMap;
use once_cell::sync::Lazy;
use std::sync::Mutex;
pub type NodeId = super::KvasirId;
pub struct BifrostRegistry {
bridges: HashMap<String, NodeId>,
geometry_cache: HashMap<NodeId, crate::Rect>,
}
impl Default for BifrostRegistry {
fn default() -> Self {
Self::new()
}
}
impl BifrostRegistry {
pub fn new() -> Self {
Self {
bridges: HashMap::new(),
geometry_cache: HashMap::new(),
}
}
pub fn get_or_create_bridge(&mut self, bridge_id: &str) -> NodeId {
*self
.bridges
.entry(bridge_id.to_string())
.or_insert_with(super::KvasirId::new)
}
pub fn update_geometry(&mut self, node_id: NodeId, rect: crate::Rect) {
self.geometry_cache.insert(node_id, rect);
}
pub fn get_geometry(&self, node_id: NodeId) -> Option<crate::Rect> {
self.geometry_cache.get(&node_id).copied()
}
}
pub fn bifrost_registry() -> std::sync::MutexGuard<'static, BifrostRegistry> {
static REGISTRY: Lazy<Mutex<BifrostRegistry>> =
Lazy::new(|| Mutex::new(BifrostRegistry::new()));
REGISTRY.lock().unwrap()
}