use std::fmt;
pub struct WatcherGuard {
name: &'static str,
sub_id: u64,
task: Option<::dioxus::core::Task>,
cleaned: bool,
}
impl WatcherGuard {
pub fn new(name: &'static str, sub_id: u64, task: Option<::dioxus::core::Task>) -> Self {
Self {
name,
sub_id,
task,
cleaned: false,
}
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn subscription_id(&self) -> u64 {
self.sub_id
}
pub fn stop(mut self) {
self.cleanup();
}
fn cleanup(&mut self) {
if self.cleaned {
return;
}
self.cleaned = true;
if let Some(task) = self.task.take() {
if ::dioxus::core::Runtime::try_current().is_some() {
let _ = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
task.cancel();
}));
}
}
crate::internal::dispatch_cleanup(self.sub_id);
}
}
impl Drop for WatcherGuard {
fn drop(&mut self) {
self.cleanup();
}
}
impl fmt::Debug for WatcherGuard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WatcherGuard")
.field("name", &self.name)
.field("sub_id", &self.sub_id)
.finish()
}
}