Skip to content

Commit

Permalink
Close sse event source when page is destroyed (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabrennie committed Jan 24, 2024
1 parent 3efec5a commit 3a772a4
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/routes/room/[room]/+page.svelte
Expand Up @@ -11,13 +11,14 @@
import { roomStore } from '$lib/stores/RoomStore';
import { enhance } from '$app/forms';
import { invalidateAll } from '$app/navigation';
import { onMount } from 'svelte';
import { onDestroy, onMount } from 'svelte';
import ArrowLeftIcon from '$lib/components/Icons/ArrowLeftIcon.svelte';
import { quintOut } from 'svelte/easing';
import { crossfade } from 'svelte/transition';
import { flip } from 'svelte/animate';
import type User from '../../../types/User';
import Confetti from '$lib/components/Confetti/Confetti.svelte';
import type RoomEventSource from '$lib/room/RoomEventSource';
const [send, receive] = crossfade({
duration: 300,
Expand All @@ -41,10 +42,11 @@
roomStore.set(data.room);
let eliminating: Choice | undefined = undefined;
let sse: RoomEventSource | undefined;
async function subscribe() {
const RoomEventSource = (await import('$lib/room/RoomEventSource')).default;
let sse = new RoomEventSource(`${window.location.pathname}/events`);
sse = new RoomEventSource(`${window.location.pathname}/events`);
sse.addEventListener('room:users:update', (event) => {
console.log('myevent', event);
Expand All @@ -60,13 +62,14 @@
sse.onerror = async (ev) => {
console.log(ev);
sse.close();
sse?.close();
await invalidateAll();
subscribe();
};
}
onMount(subscribe);
onDestroy(() => sse?.close());
function onSpinnerComplete() {
setTimeout(async () => {
Expand Down

0 comments on commit 3a772a4

Please sign in to comment.