use std::collections::HashMap;
use std::sync::Arc;
use serde_json::Value;
use tokio::sync::{oneshot, Mutex};
pub(super) struct PendingCallRegistration {
pending: Arc<Mutex<HashMap<u64, oneshot::Sender<Value>>>>,
id: u64,
}
impl PendingCallRegistration {
pub(super) fn new(pending: Arc<Mutex<HashMap<u64, oneshot::Sender<Value>>>>, id: u64) -> Self {
Self { pending, id }
}
}
impl Drop for PendingCallRegistration {
fn drop(&mut self) {
if let Ok(mut pending) = self.pending.try_lock() {
pending.remove(&self.id);
return;
}
let pending = self.pending.clone();
let id = self.id;
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime.spawn(async move {
pending.lock().await.remove(&id);
});
} else {
crate::runtime_stack::spawn(move || {
pending.blocking_lock().remove(&id);
});
}
}
}