use std::collections::VecDeque;
use std::sync::Mutex;
use super::asked::PressPoint;
const KEPT: usize = 16;
static POINTS: Mutex<VecDeque<(u32, PressPoint)>> = Mutex::new(VecDeque::new());
pub fn note(serial: u32, at: PressPoint) {
let mut points = POINTS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
points.retain(|(kept, _)| *kept != serial);
if points.len() == KEPT {
points.pop_front();
}
points.push_back((serial, at));
}
#[must_use]
pub fn of(serial: u32) -> Option<PressPoint> {
POINTS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.iter()
.rev()
.find(|(kept, _)| *kept == serial)
.map(|(_, at)| *at)
}
thread_local! {
static HANDLING: std::cell::Cell<Option<u32>> = const { std::cell::Cell::new(None) };
}
pub fn handling<R>(serial: Option<u32>, act: impl FnOnce() -> R) -> R {
struct Restore(Option<u32>);
impl Drop for Restore {
fn drop(&mut self) {
HANDLING.with(|handling| handling.set(self.0));
}
}
let _restore = Restore(HANDLING.with(|handling| handling.replace(serial)));
act()
}
#[must_use]
pub fn handled() -> Option<u32> {
HANDLING.with(std::cell::Cell::get)
}