Skip to content

Commit

Permalink
handle resubscribing for matches
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Oct 15, 2024
1 parent 130611d commit e8bc19f
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/components/Matches.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,50 @@
}
}
onMount(async () => {
if ((data.matches?.matches?.length ?? 0) > 0) await subscribeToMatches();
onMount(() => {
// Check if there are already matches and subscribe
if ((data.matches?.matches?.length ?? 0) > 0) subscribeToMatches();
// Handle visibility change (when the app comes back into focus)
document.addEventListener('visibilitychange', handleVisibilityChange);
// Handle network status changes (for offline and online handling)
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
});
onDestroy(() => {
// Unsubscribe when component is destroyed
if (matchesSubscription) matchesSubscription.unsubscribe();
// Remove event listeners
document.removeEventListener('visibilitychange', handleVisibilityChange);
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
});
async function handleVisibilityChange() {
if (
!document.hidden &&
!data.matches.subscriptionStatus &&
matchesSubscription?.state === 'closed'
) {
if (!document.hidden) {
// Check if subscription was lost, and reconnect if necessary
if (!data.matches.subscriptionStatus && matchesSubscription?.state === 'closed') {
await subscribeToMatches();
}
}
}
function handleOnline() {
// Attempt to resubscribe or reload matches when network comes back online
if (matchesSubscription?.state === 'closed') {
subscribeToMatches();
toast.success('You are back online. Reconnecting...');
}
}
function handleOffline() {
// Notify user when they go offline
toast.error('You are offline. Matches cannot be updated.');
}
async function subscribeToMatches() {
try {
matchesSubscription = await data.matches.subscribeToMatches();
Expand Down

0 comments on commit e8bc19f

Please sign in to comment.