Skip to content

Commit

Permalink
Fix watcher producing bad items
Browse files Browse the repository at this point in the history
  • Loading branch information
ralismark committed Apr 10, 2023
1 parent 5e8043d commit 94309f9
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions crates/notifier_host/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@ impl Watcher {
let (service, _) = parse_service(service, hdr, con).await?;
log::info!("new host: {}", service);

{
let added_first = {
let mut hosts = self.hosts.lock().unwrap();
if !hosts.insert(service.to_string()) {
// we're already tracking them
return Ok(())
}
}
hosts.len() == 1
};

self.is_status_notifier_host_registered_changed(&ctxt).await?;
if added_first {
self.is_status_notifier_host_registered_changed(&ctxt).await?;
}
Watcher::status_notifier_host_registered(&ctxt).await?;

self.tasks.spawn({
Expand All @@ -107,12 +110,15 @@ impl Watcher {
wait_for_service_exit(con.clone(), service.as_ref().into()).await.unwrap();
log::info!("lost host: {}", service);

{
let removed_last = {
let mut hosts = hosts.lock().unwrap();
hosts.remove(service.as_str());
}
let did_remove = hosts.remove(service.as_str());
did_remove && hosts.is_empty()
};

Watcher::is_status_notifier_host_registered_refresh(&ctxt).await.unwrap();
if removed_last {
Watcher::is_status_notifier_host_registered_refresh(&ctxt).await.unwrap();
}
Watcher::status_notifier_host_unregistered(&ctxt).await.unwrap();
}
});
Expand Down Expand Up @@ -161,7 +167,7 @@ impl Watcher {
log::info!("new item: {}", item);

self.registered_status_notifier_items_changed(&ctxt).await?;
Watcher::status_notifier_item_registered(&ctxt, service.as_ref()).await?;
Watcher::status_notifier_item_registered(&ctxt, item.as_ref()).await?;

self.tasks.spawn({
let items = self.items.clone();
Expand All @@ -177,7 +183,7 @@ impl Watcher {
}

Watcher::registered_status_notifier_items_refresh(&ctxt).await.unwrap();
Watcher::status_notifier_item_unregistered(&ctxt, service.as_ref()).await.unwrap();
Watcher::status_notifier_item_unregistered(&ctxt, item.as_ref()).await.unwrap();
}
});

Expand All @@ -186,11 +192,11 @@ impl Watcher {

/// StatusNotifierItemRegistered signal
#[dbus_interface(signal)]
async fn status_notifier_item_registered(ctxt: &zbus::SignalContext<'_>, service: zbus::names::BusName<'_>) -> zbus::Result<()>;
async fn status_notifier_item_registered(ctxt: &zbus::SignalContext<'_>, service: &str) -> zbus::Result<()>;

/// StatusNotifierItemUnregistered signal
#[dbus_interface(signal)]
async fn status_notifier_item_unregistered(ctxt: &zbus::SignalContext<'_>, service: zbus::names::BusName<'_>) -> zbus::Result<()>;
async fn status_notifier_item_unregistered(ctxt: &zbus::SignalContext<'_>, service: &str) -> zbus::Result<()>;

/// RegisteredStatusNotifierItems property
#[dbus_interface(property)]
Expand Down

0 comments on commit 94309f9

Please sign in to comment.