fix(notify): coalesce desktop syncs, notice a stopped worker
- Worker drains all queued commands per pass and applies only the latest visible set with all closes - No GetCapabilities round trip per sync - A failed send marks the worker gone, so the center falls back to its own toast windows Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -74,6 +74,7 @@ pub struct DesktopNotifications {
74
74
commands: Sender<Command>,
75
75
events: Mutex<Receiver<NotificationEvent>>,
76
76
startup: Mutex<Option<Startup>>,
77
worker_gone: AtomicBool,
77
78
}
78
79
79
80
struct Startup {
@@ -109,11 +110,16 @@ impl DesktopNotifications {
109
110
ready: ready_rx,
110
111
since: Instant::now(),
111
112
})),
113
worker_gone: AtomicBool::new(false),
112
114
})
113
115
}
114
116
115
117
/// Without a notification daemon the caller falls back to its own toast windows.
116
118
pub fn failed(&self) -> bool {
119
if self.worker_gone.load(Ordering::Relaxed) {
120
log_missing_once("notification worker stopped");
121
return true;
122
}
117
123
let Ok(mut startup) = self.startup.lock() else {
118
124
return true;
119
125
};
@@ -135,7 +141,13 @@ impl DesktopNotifications {
135
141
}
136
142
137
143
pub fn sync(&self, visible: Vec<BusNotification>, close: Vec<String>) {
138
let _ = self.commands.send(Command::Sync { visible, close });
144
if self
145
.commands
146
.send(Command::Sync { visible, close })
147
.is_err()
148
{
149
self.worker_gone.store(true, Ordering::Relaxed);
150
}
139
151
}
140
152
141
153
pub fn poll(&self) -> Vec<NotificationEvent> {
@@ -209,7 +221,7 @@ async fn worker(
209
221
210
222
let mut by_replace: HashMap<String, Published> = HashMap::new();
211
223
let mut by_dbus: HashMap<u32, String> = HashMap::new();
212
let mut supports_inline_reply = caps.iter().any(|cap| cap == "inline-reply");
224
let supports_inline_reply = caps.iter().any(|cap| cap == "inline-reply");
213
225
214
226
let mut action_stream = match receive_signal(&connection, "ActionInvoked").await {
215
227
Ok(stream) => stream,
@@ -259,24 +271,29 @@ async fn worker(
259
271
}
260
272
}
261
273
_ = tokio::time::sleep(Duration::from_millis(40)) => {
262
match commands.try_recv() {
263
Ok(Command::Shutdown) | Err(TryRecvError::Disconnected) => break,
264
Err(TryRecvError::Empty) => {}
265
Ok(Command::Sync { visible, close }) => {
266
if let Ok(caps) = get_capabilities(&connection).await {
267
supports_inline_reply = caps.iter().any(|cap| cap == "inline-reply");
274
let mut latest = None;
275
let mut close = Vec::new();
276
loop {
277
match commands.try_recv() {
278
Ok(Command::Shutdown) | Err(TryRecvError::Disconnected) => return,
279
Err(TryRecvError::Empty) => break,
280
Ok(Command::Sync { visible, close: closing }) => {
281
close.extend(closing);
282
latest = Some(visible);
268
283
}
269
apply_sync(
270
&connection,
271
&mut by_replace,
272
&mut by_dbus,
273
visible,
274
close,
275
supports_inline_reply,
276
)
277
.await;
278
284
}
279
285
}
286
if let Some(visible) = latest {
287
apply_sync(
288
&connection,
289
&mut by_replace,
290
&mut by_dbus,
291
visible,
292
close,
293
supports_inline_reply,
294
)
295
.await;
296
}
280
297
}
281
298
}
282
299
}
@@ -74,6 +74,7 @@ pub struct DesktopNotifications {74
commands: Sender<Command>,74
commands: Sender<Command>,75
events: Mutex<Receiver<NotificationEvent>>,75
events: Mutex<Receiver<NotificationEvent>>,76
startup: Mutex<Option<Startup>>,76
startup: Mutex<Option<Startup>>,77
worker_gone: AtomicBool,77
}78
}78
79
79
struct Startup {80
struct Startup {@@ -109,11 +110,16 @@ impl DesktopNotifications {109
ready: ready_rx,110
ready: ready_rx,110
since: Instant::now(),111
since: Instant::now(),111
})),112
})),113
worker_gone: AtomicBool::new(false),112
})114
})113
}115
}114
116
115
/// Without a notification daemon the caller falls back to its own toast windows.117
/// Without a notification daemon the caller falls back to its own toast windows.116
pub fn failed(&self) -> bool {118
pub fn failed(&self) -> bool {119
if self.worker_gone.load(Ordering::Relaxed) {120
log_missing_once("notification worker stopped");121
return true;122
}117
let Ok(mut startup) = self.startup.lock() else {123
let Ok(mut startup) = self.startup.lock() else {118
return true;124
return true;119
};125
};@@ -135,7 +141,13 @@ impl DesktopNotifications {135
}141
}136
142
137
pub fn sync(&self, visible: Vec<BusNotification>, close: Vec<String>) {143
pub fn sync(&self, visible: Vec<BusNotification>, close: Vec<String>) {138
let _ = self.commands.send(Command::Sync { visible, close });144
if self145
.commands146
.send(Command::Sync { visible, close })147
.is_err()148
{149
self.worker_gone.store(true, Ordering::Relaxed);150
}139
}151
}140
152
141
pub fn poll(&self) -> Vec<NotificationEvent> {153
pub fn poll(&self) -> Vec<NotificationEvent> {@@ -209,7 +221,7 @@ async fn worker(209
221
210
let mut by_replace: HashMap<String, Published> = HashMap::new();222
let mut by_replace: HashMap<String, Published> = HashMap::new();211
let mut by_dbus: HashMap<u32, String> = HashMap::new();223
let mut by_dbus: HashMap<u32, String> = HashMap::new();212
let mut supports_inline_reply = caps.iter().any(|cap| cap == "inline-reply");224
let supports_inline_reply = caps.iter().any(|cap| cap == "inline-reply");213
225
214
let mut action_stream = match receive_signal(&connection, "ActionInvoked").await {226
let mut action_stream = match receive_signal(&connection, "ActionInvoked").await {215
Ok(stream) => stream,227
Ok(stream) => stream,@@ -259,24 +271,29 @@ async fn worker(259
}271
}260
}272
}261
_ = tokio::time::sleep(Duration::from_millis(40)) => {273
_ = tokio::time::sleep(Duration::from_millis(40)) => {262
match commands.try_recv() {274
let mut latest = None;263
Ok(Command::Shutdown) | Err(TryRecvError::Disconnected) => break,275
let mut close = Vec::new();264
Err(TryRecvError::Empty) => {}276
loop {265
Ok(Command::Sync { visible, close }) => {277
match commands.try_recv() {266
if let Ok(caps) = get_capabilities(&connection).await {278
Ok(Command::Shutdown) | Err(TryRecvError::Disconnected) => return,267
supports_inline_reply = caps.iter().any(|cap| cap == "inline-reply");279
Err(TryRecvError::Empty) => break,280
Ok(Command::Sync { visible, close: closing }) => {281
close.extend(closing);282
latest = Some(visible);268
}283
}269
apply_sync(270
&connection,271
&mut by_replace,272
&mut by_dbus,273
visible,274
close,275
supports_inline_reply,276
)277
.await;278
}284
}279
}285
}286
if let Some(visible) = latest {287
apply_sync(288
&connection,289
&mut by_replace,290
&mut by_dbus,291
visible,292
close,293
supports_inline_reply,294
)295
.await;296
}280
}297
}281
}298
}282
}299
}