perf(custom_notification): debounce cache writes
This commit is contained in:
1 file changed
+24
-4
@@ -8,7 +8,7 @@ from fabric.notifications import (
|
|||||||
NotificationImagePixmap,
|
NotificationImagePixmap,
|
||||||
Notifications,
|
Notifications,
|
||||||
)
|
)
|
||||||
from fabric.utils import GdkPixbuf, logger, os, time
|
from fabric.utils import GdkPixbuf, GLib, logger, os, time
|
||||||
|
|
||||||
import shared.data as data
|
import shared.data as data
|
||||||
from services.config import config, on_config_change
|
from services.config import config, on_config_change
|
||||||
@@ -226,6 +226,7 @@ class CachedNotifications(Notifications):
|
|||||||
self._dont_disturb = False
|
self._dont_disturb = False
|
||||||
self._count = 0
|
self._count = 0
|
||||||
self._next_cache_id = 1 # Track next available cache ID
|
self._next_cache_id = 1 # Track next available cache ID
|
||||||
|
self._cache_write_source = None # Debounced JSON write source id
|
||||||
self._session_start_time = int(
|
self._session_start_time = int(
|
||||||
time.time()
|
time.time()
|
||||||
) # Track session start time for deduplication
|
) # Track session start time for deduplication
|
||||||
@@ -314,6 +315,24 @@ class CachedNotifications(Notifications):
|
|||||||
with open(NOTIFICATION_CACHE_FILE, "w") as file:
|
with open(NOTIFICATION_CACHE_FILE, "w") as file:
|
||||||
json.dump(serialized_data, file, indent=4)
|
json.dump(serialized_data, file, indent=4)
|
||||||
|
|
||||||
|
def _schedule_cache_write(self) -> None:
|
||||||
|
"""Coalesce rapid add/remove JSON writes into a single disk write.
|
||||||
|
|
||||||
|
Each full write re-serializes every cached notification (and
|
||||||
|
recomputes icon cache keys), so bursts should land on one write.
|
||||||
|
"""
|
||||||
|
if self._cache_write_source is not None:
|
||||||
|
return
|
||||||
|
self._cache_write_source = GLib.timeout_add(300, self._flush_cache_write)
|
||||||
|
|
||||||
|
def _flush_cache_write(self) -> bool:
|
||||||
|
self._cache_write_source = None
|
||||||
|
try:
|
||||||
|
self.cache_notifications()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to save notification cache: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
def clear_all_cached_notifications(self):
|
def clear_all_cached_notifications(self):
|
||||||
"""Empty the notifications with enhanced cache cleanup"""
|
"""Empty the notifications with enhanced cache cleanup"""
|
||||||
# Clean up all cached files before clearing notifications
|
# Clean up all cached files before clearing notifications
|
||||||
@@ -510,8 +529,9 @@ class CachedNotifications(Notifications):
|
|||||||
# Update cached notification with final metadata
|
# Update cached notification with final metadata
|
||||||
self._cached_notifications[cache_id] = cached_notification
|
self._cached_notifications[cache_id] = cached_notification
|
||||||
|
|
||||||
# Save updated metadata to JSON
|
# Save updated metadata to JSON (debounced - the guaranteed first
|
||||||
self.cache_notifications()
|
# write above already persisted the notification itself)
|
||||||
|
self._schedule_cache_write()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(
|
logger.error(
|
||||||
@@ -547,7 +567,7 @@ class CachedNotifications(Notifications):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
self.cache_notifications() # Update JSON
|
self._schedule_cache_write() # Update JSON (debounced)
|
||||||
self._count -= 1
|
self._count -= 1
|
||||||
self.notify("count")
|
self.notify("count")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user