fix: crash in notificatio fetcher

This commit is contained in:
Jones8683
2025-11-10 17:10:53 +10:30
parent 466628479e
commit 7136de90be
@@ -39,7 +39,7 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
"[class*='notifications__bubble___']", "[class*='notifications__bubble___']",
) as HTMLElement; ) as HTMLElement;
if (api.storage.lastNotificationCount !== 0) { if (alertDiv && api.storage.lastNotificationCount !== 0) {
alertDiv.textContent = api.storage.lastNotificationCount.toString(); alertDiv.textContent = api.storage.lastNotificationCount.toString();
} }
@@ -74,13 +74,17 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
} }
} catch (error) { } catch (error) {
console.error("[BetterSEQTA+] Error fetching notifications:", error); console.error("[BetterSEQTA+] Error fetching notifications:", error);
api.storage.consecutiveErrors = (api.storage.consecutiveErrors || 0) + 1; api.storage.consecutiveErrors =
(api.storage.consecutiveErrors || 0) + 1;
} }
}; };
const getNextInterval = () => { const getNextInterval = () => {
// Exponential backoff on errors, max 5 minutes // Exponential backoff on errors, max 5 minutes
const errorMultiplier = Math.min(Math.pow(2, api.storage.consecutiveErrors || 0), 10); const errorMultiplier = Math.min(
Math.pow(2, api.storage.consecutiveErrors || 0),
10,
);
return Math.min(baseInterval * errorMultiplier, maxInterval); return Math.min(baseInterval * errorMultiplier, maxInterval);
}; };
@@ -92,7 +96,8 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
const interval = getNextInterval(); const interval = getNextInterval();
pollInterval = window.setTimeout(() => { pollInterval = window.setTimeout(() => {
checkNotifications().then(() => { checkNotifications().then(() => {
if (pollInterval) { // Only continue if not stopped if (pollInterval) {
// Only continue if not stopped
scheduleNext(); scheduleNext();
} }
}); });
@@ -124,14 +129,16 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
isVisible = !document.hidden; isVisible = !document.hidden;
if (isVisible && !pollInterval) { if (isVisible && !pollInterval) {
// Resume polling when tab becomes visible // Resume polling when tab becomes visible
const alertDiv = document.querySelector("[class*='notifications__bubble___']"); const alertDiv = document.querySelector(
"[class*='notifications__bubble___']",
);
if (alertDiv) { if (alertDiv) {
startPolling(); startPolling();
} }
} }
}; };
document.addEventListener('visibilitychange', handleVisibilityChange); document.addEventListener("visibilitychange", handleVisibilityChange);
api.seqta.onMount("[class*='notifications__bubble___']", (_) => { api.seqta.onMount("[class*='notifications__bubble___']", (_) => {
startPolling(); startPolling();
@@ -139,7 +146,7 @@ const notificationCollectorPlugin: Plugin<{}, NotificationCollectorStorage> = {
return () => { return () => {
stopPolling(); stopPolling();
document.removeEventListener('visibilitychange', handleVisibilityChange); document.removeEventListener("visibilitychange", handleVisibilityChange);
}; };
}, },
}; };