use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::RwLock;
pub struct Notify(RwLock<Option<Sender<()>>>, RwLock<Option<Receiver<()>>>);
impl Notify {
pub fn new() -> Self {
let (send, recv) = tokio::sync::mpsc::channel(1);
Self(RwLock::new(Some(send)), RwLock::new(Some(recv)))
}
pub async fn notify(&self) -> bool {
let mut sender = self.0.write().await;
if let Some(_) = sender.take() {
true
} else {
false
}
}
pub async fn wait(&self) -> bool {
let mut receiver = self.1.write().await;
if let Some(receiver) = receiver.as_mut() {
receiver.recv().await;
true
} else {
false
}
}
}