use std::sync::Arc;
use tokio::sync::watch;
#[derive(Clone)]
pub struct ShutdownSignal(Arc<watch::Sender<bool>>);
impl ShutdownSignal {
pub fn new() -> Self {
Self(Arc::new(watch::Sender::new(false)))
}
pub fn trigger(&self) {
self.0.send_replace(true);
}
pub async fn wait(&self) {
let mut rx = self.0.subscribe();
while rx.wait_for(|shutting_down| *shutting_down).await.is_ok() {}
}
pub async fn wait_owned(self) {
self.wait().await
}
}