use std::cell::RefCell;
use std::rc::Rc;
use crate::actor::registry::Owner;
use crate::trace;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Change {
ActorAdded {
root: Option<u64>,
id: usize,
type_name: &'static str,
owner: Owner,
},
ActorRemoved {
root: Option<u64>,
id: usize,
},
ActorHandled { id: usize },
TimerStarted { id: u64 },
TimerStopped { id: u64 },
Subscriptions { bus: trace::Bus, root: Option<u64> },
RouterOpened { root: u64 },
RouterClosed { root: u64 },
}
type Watcher = Rc<dyn Fn(&Change)>;
thread_local! {
static WATCHER: RefCell<Option<Watcher>> = const { RefCell::new(None) };
}
pub fn watch(watcher: impl Fn(&Change) + 'static) {
WATCHER.with(|slot| slot.borrow_mut().replace(Rc::new(watcher)));
}
pub fn stop_watching() {
WATCHER.with(|slot| slot.borrow_mut().take());
}
pub fn changed(change: impl FnOnce() -> Change) {
let Some(watcher) = WATCHER.try_with(|slot| slot.borrow().clone()).ok().flatten() else {
return;
};
watcher(&change());
}