From 6930ce9a7c03c03cad87be41f2e04353cb19ac30 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Wed, 28 Aug 2024 00:20:44 +0800 Subject: [PATCH] Debounce page visibility to prevent streaming/polling noise --- src/components/background-service.jsx | 13 ++++++++++++- src/locales/en.po | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/background-service.jsx b/src/components/background-service.jsx index 5b9a7ef1b..641448327 100644 --- a/src/components/background-service.jsx +++ b/src/components/background-service.jsx @@ -16,7 +16,18 @@ export default memo(function BackgroundService({ isLoggedIn }) { // Notifications service // - WebSocket to receive notifications when page is visible const [visible, setVisible] = useState(true); - usePageVisibility(setVisible); + const visibleTimeout = useRef(); + usePageVisibility((visible) => { + clearTimeout(visibleTimeout.current); + if (visible) { + setVisible(true); + } else { + visibleTimeout.current = setTimeout(() => { + setVisible(false); + }, POLL_INTERVAL); + } + }); + const checkLatestNotification = async (masto, instance, skipCheckMarkers) => { if (states.notificationsLast) { const notificationsIterator = masto.v1.notifications.list({ diff --git a/src/locales/en.po b/src/locales/en.po index fd83a8d0d..6a399c6f4 100644 --- a/src/locales/en.po +++ b/src/locales/en.po @@ -511,11 +511,11 @@ msgstr "" msgid "server domain name" msgstr "" -#: src/components/background-service.jsx:138 +#: src/components/background-service.jsx:149 msgid "Cloak mode disabled" msgstr "" -#: src/components/background-service.jsx:138 +#: src/components/background-service.jsx:149 msgid "Cloak mode enabled" msgstr ""