1
0
Fork 0

Revert "Debounce checks, less noisy"

This reverts commit 9c4252315a.
This commit is contained in:
Lim Chee Aun 2023-10-30 20:45:30 +08:00
parent 5f64553d17
commit 99f81c49c4
2 changed files with 45 additions and 59 deletions

View file

@ -1,6 +1,5 @@
import { memo } from 'preact/compat'; import { memo } from 'preact/compat';
import { useEffect, useRef, useState } from 'preact/hooks'; import { useEffect, useRef, useState } from 'preact/hooks';
import { useDebouncedCallback } from 'use-debounce';
import { api } from '../utils/api'; import { api } from '../utils/api';
import states, { saveStatus } from '../utils/states'; import states, { saveStatus } from '../utils/states';
@ -12,62 +11,55 @@ export default memo(function BackgroundService({ isLoggedIn }) {
// - WebSocket to receive notifications when page is visible // - WebSocket to receive notifications when page is visible
const [visible, setVisible] = useState(true); const [visible, setVisible] = useState(true);
usePageVisibility(setVisible); usePageVisibility(setVisible);
const subRef = useRef(); useEffect(() => {
const debouncedStartNotifications = useDebouncedCallback(() => { let sub;
const { masto, streaming, instance } = api(); if (isLoggedIn && visible) {
(async () => { const { masto, streaming, instance } = api();
// 1. Get the latest notification (async () => {
if (states.notificationsLast) { // 1. Get the latest notification
const notificationsIterator = masto.v1.notifications.list({ if (states.notificationsLast) {
limit: 1, const notificationsIterator = masto.v1.notifications.list({
since_id: states.notificationsLast.id, limit: 1,
}); since_id: states.notificationsLast.id,
const { value: notifications } = await notificationsIterator.next(); });
if (notifications?.length) { const { value: notifications } = await notificationsIterator.next();
let lastReadId; if (notifications?.length) {
try { let lastReadId;
const markers = await masto.v1.markers.fetch({ try {
timeline: 'notifications', const markers = await masto.v1.markers.fetch({
}); timeline: 'notifications',
lastReadId = markers?.notifications?.lastReadId; });
} catch (e) {} lastReadId = markers?.notifications?.lastReadId;
if (lastReadId) { } catch (e) {}
states.notificationsShowNew = notifications[0].id !== lastReadId; if (lastReadId) {
} else { 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; 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 () => { return () => {
// sub?.unsubscribe?.(); sub?.unsubscribe?.();
// sub = null; sub = null;
debouncedStartNotifications?.cancel?.();
subRef.current?.unsubscribe?.();
subRef.current = null;
}; };
}, [visible, isLoggedIn]); }, [visible, isLoggedIn]);

View file

@ -252,10 +252,6 @@ function Timeline({
}, },
[id, loadItems, checkForUpdates, snapStates.settings.autoRefresh], [id, loadItems, checkForUpdates, snapStates.settings.autoRefresh],
); );
const debouncedLoadOrCheckUpdates = useDebouncedCallback(
loadOrCheckUpdates,
3000,
);
const lastHiddenTime = useRef(); const lastHiddenTime = useRef();
usePageVisibility( usePageVisibility(
@ -263,14 +259,12 @@ function Timeline({
if (visible) { if (visible) {
const timeDiff = Date.now() - lastHiddenTime.current; const timeDiff = Date.now() - lastHiddenTime.current;
if (!lastHiddenTime.current || timeDiff > 1000 * 60) { if (!lastHiddenTime.current || timeDiff > 1000 * 60) {
// 1 minute loadOrCheckUpdates({
debouncedLoadOrCheckUpdates({
disableIdleCheck: true, disableIdleCheck: true,
}); });
} }
} else { } else {
lastHiddenTime.current = Date.now(); lastHiddenTime.current = Date.now();
debouncedLoadOrCheckUpdates.cancel();
} }
setVisible(visible); setVisible(visible);
}, },