this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Rearrange/code this part again

- Streaming wasn't UNSUBscribed due to the forever-stuck loop
- Make streaming start later

+33 -25
+33 -25
src/components/background-service.jsx
··· 8 8 import useInterval from '../utils/useInterval'; 9 9 import usePageVisibility from '../utils/usePageVisibility'; 10 10 11 + const STREAMING_TIMEOUT = 1000 * 3; // 3 seconds 12 + const POLL_INTERVAL = 1000 * 60; // 1 minute 13 + 11 14 export default memo(function BackgroundService({ isLoggedIn }) { 12 15 // Notifications service 13 16 // - WebSocket to receive notifications when page is visible ··· 53 56 let hasStreaming = false; 54 57 // 2. Start streaming 55 58 if (streaming) { 56 - try { 57 - hasStreaming = true; 58 - sub = streaming.user.notification.subscribe(); 59 - console.log('🎏 Streaming notification', sub); 60 - for await (const entry of sub) { 61 - if (!sub) break; 62 - console.log('🔔🔔 Notification entry', entry); 63 - if (entry.event === 'notification') { 64 - console.log('🔔🔔 Notification', entry); 65 - saveStatus(entry.payload, instance, { 66 - skipThreading: true, 67 - }); 59 + pollNotifications = setTimeout(() => { 60 + (async () => { 61 + try { 62 + hasStreaming = true; 63 + sub = streaming.user.notification.subscribe(); 64 + console.log('🎏 Streaming notification', sub); 65 + for await (const entry of sub) { 66 + if (!sub) break; 67 + if (!visible) break; 68 + console.log('🔔🔔 Notification entry', entry); 69 + if (entry.event === 'notification') { 70 + console.log('🔔🔔 Notification', entry); 71 + saveStatus(entry.payload, instance, { 72 + skipThreading: true, 73 + }); 74 + } 75 + states.notificationsShowNew = true; 76 + } 77 + } catch (e) { 78 + hasStreaming = false; 79 + console.error(e); 68 80 } 69 - states.notificationsShowNew = true; 70 - } 71 - } catch (e) { 72 - hasStreaming = false; 73 - console.error(e); 74 - } 75 - } 76 81 77 - if (!hasStreaming) { 78 - console.log('🎏 Streaming failed, fallback to polling'); 79 - // Fallback to polling every minute 80 - pollNotifications = setInterval(() => { 81 - checkLatestNotification(masto, instance, true); 82 - }, 1000 * 60); 82 + if (!hasStreaming) { 83 + console.log('🎏 Streaming failed, fallback to polling'); 84 + pollNotifications = setInterval(() => { 85 + checkLatestNotification(masto, instance, true); 86 + }, POLL_INTERVAL); 87 + } 88 + })(); 89 + }, STREAMING_TIMEOUT); 83 90 } 84 91 })(); 85 92 } 86 93 return () => { 87 94 sub?.unsubscribe?.(); 88 95 sub = null; 96 + clearTimeout(pollNotifications); 89 97 clearInterval(pollNotifications); 90 98 }; 91 99 }, [visible, isLoggedIn]);