use std::sync::Arc;
use crate::handle::NodeId;
#[derive(Debug, Clone)]
pub enum TopologyEvent {
NodeRegistered(NodeId),
NodeTornDown(NodeId),
DepsChanged {
node: NodeId,
old_deps: Vec<NodeId>,
new_deps: Vec<NodeId>,
},
}
pub type TopologySink = Arc<dyn Fn(&TopologyEvent)>;
pub type TopologySubscriptionId = u64;
pub(crate) fn unsubscribe_topology_sink(core: &crate::node::Core, id: u64) {
let mut s = crate::node::St::new(core);
s.shared.topology_sinks.remove(&id);
}
impl super::node::Core {
pub fn subscribe_topology(&self, sink: TopologySink) -> TopologySubscriptionId {
let mut s = self.lock_state();
let id = s.shared.next_topology_id;
s.shared.next_topology_id += 1;
s.shared.topology_sinks.insert(id, sink);
id
}
pub fn unsubscribe_topology(&self, id: TopologySubscriptionId) {
unsubscribe_topology_sink(self, id);
}
pub(crate) fn fire_topology_event(&self, event: &TopologyEvent) {
let sinks: Vec<TopologySink> =
self.with_shared(|sh| sh.topology_sinks.values().cloned().collect());
for sink in sinks {
sink(event);
}
}
}