use std::future::Future;
use iced_futures::MaybeSend;
use tokio::sync::oneshot;
#[derive(Default)]
#[repr(transparent)]
pub(crate) struct Singleton {
tx: Option<oneshot::Sender<()>>,
}
impl Singleton {
pub(crate) fn is_set(&self) -> bool {
self.tx.is_some()
}
pub(crate) fn clear(&mut self) {
self.tx = None;
}
pub(crate) fn set<F>(&mut self, future: F) -> impl Future<Output = Option<F::Output>>
where
F: Future + MaybeSend + 'static,
{
let (tx, rx) = oneshot::channel();
self.tx = Some(tx);
async move {
tokio::select! {
_ = rx => None,
output = future => Some(output),
}
}
}
}