use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
#[derive(Default)]
pub struct InstallCancelRegistry {
tokens: Mutex<HashMap<String, Arc<AtomicBool>>>,
}
impl InstallCancelRegistry {
pub fn new() -> Self {
Self {
tokens: Mutex::new(HashMap::new()),
}
}
pub fn register(&self, key: &str) -> Arc<AtomicBool> {
let token = Arc::new(AtomicBool::new(false));
self.tokens
.lock()
.unwrap()
.insert(key.to_string(), token.clone());
token
}
pub fn cancel(&self, key: &str) -> bool {
if let Some(token) = self.tokens.lock().unwrap().get(key) {
token.store(true, Ordering::SeqCst);
true
} else {
false
}
}
pub fn remove(&self, key: &str) {
self.tokens.lock().unwrap().remove(key);
}
}
#[cfg(test)]
mod tests;