From 99f81c49c41d50766fd8338e0bca464511ae0b68 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Mon, 30 Oct 2023 20:45:30 +0800 Subject: [PATCH] Revert "Debounce checks, less noisy" This reverts commit 9c4252315a508f33f2b4e4c36b21066937f852f0. --- src/components/background-service.jsx | 96 ++++++++++++--------------- src/components/timeline.jsx | 8 +-- 2 files changed, 45 insertions(+), 59 deletions(-) diff --git a/src/components/background-service.jsx b/src/components/background-service.jsx index e7d7dcf..927a174 100644 --- a/src/components/background-service.jsx +++ b/src/components/background-service.jsx @@ -1,6 +1,5 @@ import { memo } from 'preact/compat'; import { useEffect, useRef, useState } from 'preact/hooks'; -import { useDebouncedCallback } from 'use-debounce'; import { api } from '../utils/api'; import states, { saveStatus } from '../utils/states'; @@ -12,62 +11,55 @@ export default memo(function BackgroundService({ isLoggedIn }) { // - WebSocket to receive notifications when page is visible const [visible, setVisible] = useState(true); usePageVisibility(setVisible); - const subRef = useRef(); - const debouncedStartNotifications = useDebouncedCallback(() => { - const { masto, streaming, instance } = api(); - (async () => { - // 1. Get the latest notification - if (states.notificationsLast) { - const notificationsIterator = masto.v1.notifications.list({ - limit: 1, - since_id: states.notificationsLast.id, - }); - const { value: notifications } = await notificationsIterator.next(); - if (notifications?.length) { - let lastReadId; - try { - const markers = await masto.v1.markers.fetch({ - timeline: 'notifications', - }); - lastReadId = markers?.notifications?.lastReadId; - } catch (e) {} - if (lastReadId) { - states.notificationsShowNew = notifications[0].id !== lastReadId; - } else { + useEffect(() => { + let sub; + if (isLoggedIn && visible) { + const { masto, streaming, instance } = api(); + (async () => { + // 1. Get the latest notification + if (states.notificationsLast) { + const notificationsIterator = masto.v1.notifications.list({ + limit: 1, + since_id: states.notificationsLast.id, + }); + const { value: notifications } = await notificationsIterator.next(); + if (notifications?.length) { + let lastReadId; + try { + const markers = await masto.v1.markers.fetch({ + timeline: 'notifications', + }); + lastReadId = markers?.notifications?.lastReadId; + } catch (e) {} + if (lastReadId) { + states.notificationsShowNew = notifications[0].id !== lastReadId; + } else { + states.notificationsShowNew = true; + } + } + } + + // 2. Start streaming + if (streaming) { + sub = streaming.user.notification.subscribe(); + console.log('🎏 Streaming notification', sub); + for await (const entry of sub) { + if (!sub) break; + console.log('🔔🔔 Notification entry', entry); + if (entry.event === 'notification') { + console.log('🔔🔔 Notification', entry); + saveStatus(entry.payload, instance, { + skipThreading: true, + }); + } states.notificationsShowNew = true; } } - } - - // 2. Start streaming - if (streaming) { - let sub = (subRef.current = streaming.user.notification.subscribe()); - console.log('🎏 Streaming notification', sub); - for await (const entry of sub) { - if (!sub) break; - console.log('🔔🔔 Notification entry', entry); - if (entry.event === 'notification') { - console.log('🔔🔔 Notification', entry); - saveStatus(entry.payload, instance, { - skipThreading: true, - }); - } - states.notificationsShowNew = true; - } - } - })(); - }, 3000); - useEffect(() => { - // let sub; - if (isLoggedIn && visible) { - debouncedStartNotifications(); + })(); } return () => { - // sub?.unsubscribe?.(); - // sub = null; - debouncedStartNotifications?.cancel?.(); - subRef.current?.unsubscribe?.(); - subRef.current = null; + sub?.unsubscribe?.(); + sub = null; }; }, [visible, isLoggedIn]); diff --git a/src/components/timeline.jsx b/src/components/timeline.jsx index ecbe693..9d05210 100644 --- a/src/components/timeline.jsx +++ b/src/components/timeline.jsx @@ -252,10 +252,6 @@ function Timeline({ }, [id, loadItems, checkForUpdates, snapStates.settings.autoRefresh], ); - const debouncedLoadOrCheckUpdates = useDebouncedCallback( - loadOrCheckUpdates, - 3000, - ); const lastHiddenTime = useRef(); usePageVisibility( @@ -263,14 +259,12 @@ function Timeline({ if (visible) { const timeDiff = Date.now() - lastHiddenTime.current; if (!lastHiddenTime.current || timeDiff > 1000 * 60) { - // 1 minute - debouncedLoadOrCheckUpdates({ + loadOrCheckUpdates({ disableIdleCheck: true, }); } } else { lastHiddenTime.current = Date.now(); - debouncedLoadOrCheckUpdates.cancel(); } setVisible(visible); },