use crate::event::SentStatus;
use std::future::Future;
use tokio_util::sync::{
CancellationToken, DropGuard, WaitForCancellationFuture, WaitForCancellationFutureOwned,
};
use tracing::warn;
#[derive(Debug, Clone, Default)]
pub struct ShutdownToken {
inner: CancellationToken,
}
impl From<CancellationToken> for ShutdownToken {
fn from(inner: CancellationToken) -> Self {
ShutdownToken { inner }
}
}
impl ShutdownToken {
#[deprecated]
#[track_caller]
pub fn send_status_msg(&self, status: SentStatus) {
let caller = std::panic::Location::caller();
warn!("{caller} attempted to send {status} - there are no more listeners of those");
}
pub fn new() -> Self {
ShutdownToken {
inner: CancellationToken::new(),
}
}
pub fn new_from_tokio_token(cancellation_token: CancellationToken) -> Self {
ShutdownToken {
inner: cancellation_token,
}
}
pub fn inner(&self) -> &CancellationToken {
&self.inner
}
pub fn to_cancellation_token(&self) -> CancellationToken {
self.inner.clone()
}
pub fn child_token(&self) -> ShutdownToken {
ShutdownToken {
inner: self.inner.child_token(),
}
}
pub fn cancel(&self) {
self.inner.cancel();
}
pub fn is_cancelled(&self) -> bool {
self.inner.is_cancelled()
}
pub fn cancelled(&self) -> WaitForCancellationFuture<'_> {
self.inner.cancelled()
}
pub fn cancelled_owned(self) -> WaitForCancellationFutureOwned {
self.inner.cancelled_owned()
}
pub fn drop_guard(self) -> ShutdownDropGuard {
ShutdownDropGuard {
inner: self.inner.drop_guard(),
}
}
pub async fn run_until_cancelled<F>(&self, fut: F) -> Option<F::Output>
where
F: Future,
{
self.inner.run_until_cancelled(fut).await
}
pub async fn run_until_cancelled_owned<F>(self, fut: F) -> Option<F::Output>
where
F: Future,
{
self.inner.run_until_cancelled_owned(fut).await
}
}
pub struct ShutdownDropGuard {
inner: DropGuard,
}
impl ShutdownDropGuard {
pub fn disarm(self) -> ShutdownToken {
ShutdownToken {
inner: self.inner.disarm(),
}
}
}