use std::cell::RefCell;
use std::collections::BTreeSet;
thread_local! {
static ONCE: RefCell<BTreeSet<String>> = const { RefCell::new(BTreeSet::new()) };
static TAKEN: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
}
pub fn mark(step: &str) {
let name = format!("plexus-ui:{step}");
#[cfg(all(target_arch = "wasm32", feature = "dom"))]
if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
let _ = p.mark(&name);
}
TAKEN.with(|t| t.borrow_mut().push(name));
}
pub fn now() -> f64 {
#[cfg(all(target_arch = "wasm32", feature = "dom"))]
if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
return p.now();
}
TAKEN.with(|t| t.borrow().len() as f64)
}
pub fn once_at(step: &str, at: f64) {
if !ONCE.with(|o| o.borrow_mut().insert(step.to_string())) {
return;
}
let name = format!("plexus-ui:{step}");
#[cfg(all(target_arch = "wasm32", feature = "dom"))]
if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
let opts = js_sys::Object::new();
let _ = js_sys::Reflect::set(&opts, &"startTime".into(), &at.into());
if let Ok(f) = js_sys::Reflect::get(&p, &"mark".into()) {
let _ = js_sys::Function::from(f).call2(&p, &name.clone().into(), &opts);
}
}
let _ = at;
TAKEN.with(|t| t.borrow_mut().push(name));
}
pub fn once(step: &str) {
if ONCE.with(|o| o.borrow_mut().insert(step.to_string())) {
mark(step);
}
}
pub fn marked(step: &str) -> bool {
ONCE.with(|o| o.borrow().contains(step))
}
pub fn taken() -> Vec<String> {
TAKEN.with(|t| t.borrow().clone())
}