use std::cell::RefCell;
use std::collections::HashMap;
thread_local! {
static POSITIONS: RefCell<HashMap<String, f64>> = RefCell::new(HashMap::new());
}
pub fn save(key: &str, top: f64) {
POSITIONS.with(|m| {
m.borrow_mut().insert(key.to_string(), top);
});
}
pub fn get(key: &str) -> f64 {
POSITIONS.with(|m| m.borrow().get(key).copied().unwrap_or(0.0))
}
pub fn restore_eval(el_id: &str, key: &str) -> String {
let pos = get(key);
format!(
"requestAnimationFrame(() => {{ let el = document.getElementById('{el_id}'); \
if (el) el.scrollTop = {pos}; }});"
)
}