use std::cell::RefCell;
type Hook = (&'static str, Box<dyn FnOnce()>);
thread_local! {
static HOOKS: RefCell<Vec<Hook>> = const { RefCell::new(Vec::new()) };
}
pub fn set(point: &'static str, run: impl FnOnce() + 'static) {
HOOKS.with(|hooks| hooks.borrow_mut().push((point, Box::new(run))));
}
pub fn clear(point: &'static str) {
HOOKS.with(|hooks| hooks.borrow_mut().retain(|(p, _)| *p != point));
}
pub fn at(point: &'static str) {
let run = HOOKS.with(|hooks| {
let mut hooks = hooks.borrow_mut();
let found = hooks.iter().position(|(p, _)| *p == point)?;
Some(hooks.remove(found).1)
});
if let Some(run) = run {
run();
}
}