Update polling

This commit is contained in:
binwiederhier
2026-01-07 09:46:08 -05:00
parent 2dd152df3f
commit bfbe73aea3
2 changed files with 32 additions and 6 deletions

View File

@@ -42,12 +42,22 @@ class Poller {
const since = subscription.last; const since = subscription.last;
const notifications = await api.poll(subscription.baseUrl, subscription.topic, since); const notifications = await api.poll(subscription.baseUrl, subscription.topic, since);
if (!notifications || notifications.length === 0) { const deletedSids = this.deletedSids(notifications);
console.log(`[Poller] No new notifications found for ${subscription.id}`); const newOrUpdatedNotifications = this.newOrUpdatedNotifications(notifications, deletedSids);
return;
// Delete all existing notifications with a deleted sequence ID
if (deletedSids.length > 0) {
console.log(`[Poller] Deleting notifications with deleted sequence IDs for ${subscription.id}`);
await Promise.all(deletedSids.map((sid) => subscriptionManager.deleteNotificationBySid(subscription.id, sid)));
}
// Add new or updated notifications
if (newOrUpdatedNotifications.length > 0) {
console.log(`[Poller] Adding ${notifications.length} notification(s) for ${subscription.id}`);
await subscriptionManager.addNotifications(subscription.id, notifications);
} else {
console.log(`[Poller] No new notifications found for ${subscription.id}`);
} }
console.log(`[Poller] Adding ${notifications.length} notification(s) for ${subscription.id}`);
await subscriptionManager.addNotifications(subscription.id, notifications);
} }
pollInBackground(subscription) { pollInBackground(subscription) {
@@ -59,6 +69,22 @@ class Poller {
} }
})(); })();
} }
deletedSids(notifications) {
return new Set(
notifications
.filter(n => n.sid && n.deleted)
.map(n => n.sid)
);
}
newOrUpdatedNotifications(notifications, deletedSids) {
return notifications
.filter((notification) => {
const sid = notification.sid || notification.id;
return !deletedSids.has(notification.id) && !deletedSids.has(sid) && !notification.deleted;
});
}
} }
const poller = new Poller(); const poller = new Poller();

View File

@@ -193,7 +193,7 @@ class SubscriptionManager {
/** Adds notification, or returns false if it already exists */ /** Adds notification, or returns false if it already exists */
async addNotification(subscriptionId, notification) { async addNotification(subscriptionId, notification) {
const exists = await this.db.notifications.get(notification.id); const exists = await this.db.notifications.get(notification.id);
if (exists) { if (exists || notification.deleted) {
return false; return false;
} }
try { try {