use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use std::sync::atomic::{AtomicU64, Ordering};
pub static BIFROST_REGISTRY: OnceLock<Arc<Mutex<BifrostRegistry>>> = OnceLock::new();
pub fn bifrost_registry() -> Arc<Mutex<BifrostRegistry>> {
BIFROST_REGISTRY.get_or_init(|| Arc::new(Mutex::new(BifrostRegistry::new()))).clone()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(dead_code)]
pub struct NodeId(u64);
static NEXT_NODE_ID: AtomicU64 = AtomicU64::new(1);
impl NodeId {
pub fn generate() -> Self {
Self(NEXT_NODE_ID.fetch_add(1, Ordering::SeqCst))
}
}
#[allow(dead_code)]
pub struct BifrostRegistry {
bridges: HashMap<String, NodeId>,
geometry_cache: HashMap<NodeId, crate::Rect>,
}
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(NodeId::generate)
}
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()
}
}