@@ -154,10 +163,10 @@
class:opacity-20={hovered}
class:dark:opacity-15={hovered}
>
- {#if initData}
+ {#if data}
{:else}
diff --git a/svelte-app/src/components/experiments/toru.ts b/svelte-app/src/components/experiments/toru.ts
index fe3815cbd..decb859bc 100644
--- a/svelte-app/src/components/experiments/toru.ts
+++ b/svelte-app/src/components/experiments/toru.ts
@@ -20,27 +20,28 @@ let socketInstance: WebSocket | undefined,
const interval = 36_000;
-export const initSync = (onUpdate: (data: ToruData) => void) => {
- if (!browser || (socketInstance && socketInstance.readyState === WebSocket.OPEN)) {
- return;
- }
-
- clearInterval(repeat);
+const onOpen = () => {
+ Logger.info('[ToruSync] Connected');
+ retries = 0;
+};
- socketInstance = new WebSocket('wss://toru.kio.dev/api/v1/ws/kiosion?cover_size=large');
+const onClose = () => {
+ Logger.info('[ToruSync] Disconnected');
+ stop();
+};
- socketInstance.addEventListener('open', () => {
- Logger.info('[ToruSync] Connected');
- retries = 0;
- });
+const onError = (e: Event) => {
+ Logger.error('[ToruSync] Error', e);
+};
- socketInstance.addEventListener('message', (event: MessageEvent
) => {
- if (!event.data || event.data === 'pong') {
+const onMessage =
+ (onUpdate: (data: ToruData) => void) => (e: MessageEvent) => {
+ if (!e.data || e.data === 'pong') {
return;
}
try {
- const res = JSON.parse(event.data as string) as ToruData;
+ const res = JSON.parse(e.data as string) as ToruData;
Logger.info('[ToruSync] Received frame');
@@ -50,26 +51,54 @@ export const initSync = (onUpdate: (data: ToruData) => void) => {
} catch (e) {
Logger.error('[ToruSync] Error parsing', e);
}
- });
+ };
- socketInstance.addEventListener('error', (e) => {
- Logger.error('[ToruSync] Error', e);
- });
+export const initSync = (onUpdate: (data: ToruData) => void) => {
+ if (!browser || (socketInstance && socketInstance.readyState === WebSocket.OPEN)) {
+ return;
+ }
+
+ clearInterval(repeat);
+
+ socketInstance = new WebSocket('wss://toru.kio.dev/api/v1/ws/kiosion?cover_size=large');
+
+ // socketInstance.addEventListener('open', () => {
+ // Logger.info('[ToruSync] Connected');
+ // retries = 0;
+ // });
- socketInstance.addEventListener('close', () => {
- Logger.info('[ToruSync] Disconnected');
+ // socketInstance.addEventListener('message', (event: MessageEvent) => {
+ // if (!event.data || event.data === 'pong') {
+ // return;
+ // }
- stop();
+ // try {
+ // const res = JSON.parse(event.data as string) as ToruData;
- // TODO: Don't run when the socket is closed intentionally
- // if (retries < 5) {
- // retries++;
- // clearTimeout(retry);
- // retry = setTimeout(() => {
- // initSync(onUpdate);
- // }, retries * 1000);
- // }
- });
+ // Logger.info('[ToruSync] Received frame');
+
+ // if (res.title || res.album || res.artist) {
+ // onUpdate(res);
+ // }
+ // } catch (e) {
+ // Logger.error('[ToruSync] Error parsing', e);
+ // }
+ // });
+
+ // socketInstance.addEventListener('error', (e) => {
+ // Logger.error('[ToruSync] Error', e);
+ // });
+
+ // socketInstance.addEventListener('close', () => {
+ // Logger.info('[ToruSync] Disconnected');
+
+ // stop();
+ // });
+
+ socketInstance.addEventListener('open', onOpen);
+ socketInstance.addEventListener('message', onMessage(onUpdate));
+ socketInstance.addEventListener('error', onError);
+ socketInstance.addEventListener('close', onClose);
repeat = setInterval(() => {
if (socketInstance && socketInstance.readyState === WebSocket.OPEN) {
@@ -78,7 +107,7 @@ export const initSync = (onUpdate: (data: ToruData) => void) => {
}, interval);
};
-export const stopSync = () => {
+export const stopSync = (onUpdate: (data: ToruData) => void) => {
if (!browser || !socketInstance) {
return;
}
@@ -89,5 +118,10 @@ export const stopSync = () => {
socketInstance.close();
}
+ socketInstance.removeEventListener('open', onOpen);
+ socketInstance.removeEventListener('message', onMessage(onUpdate));
+ socketInstance.removeEventListener('error', onError);
+ socketInstance.removeEventListener('close', onClose);
+
socketInstance = undefined;
};
diff --git a/svelte-app/src/lib/consts.ts b/svelte-app/src/lib/consts.ts
index d87d07f80..14d32b31a 100644
--- a/svelte-app/src/lib/consts.ts
+++ b/svelte-app/src/lib/consts.ts
@@ -18,6 +18,8 @@ export const LOCAL_SETTINGS_KEY = 'kio-dev-settings';
export const BASE_PAGE_TITLE = 'kio.dev';
+export const TORU_API_URL = 'https://toru.kio.dev/api/v1';
+
interface AppRoute {
name: string;
path: string;
diff --git a/svelte-app/src/routes/[[lang=lang]]/experiments/+page.svelte b/svelte-app/src/routes/[[lang=lang]]/experiments/+page.svelte
index 3482658a1..9f6ca33ff 100644
--- a/svelte-app/src/routes/[[lang=lang]]/experiments/+page.svelte
+++ b/svelte-app/src/routes/[[lang=lang]]/experiments/+page.svelte
@@ -27,7 +27,7 @@
-
+
diff --git a/svelte-app/src/routes/[[lang=lang]]/experiments/+page.ts b/svelte-app/src/routes/[[lang=lang]]/experiments/+page.ts
index 7050d5431..53dc5a2ee 100644
--- a/svelte-app/src/routes/[[lang=lang]]/experiments/+page.ts
+++ b/svelte-app/src/routes/[[lang=lang]]/experiments/+page.ts
@@ -1,19 +1,28 @@
+import { TORU_API_URL } from '$lib/consts';
+import Logger from '$lib/logger';
+
import type { PageLoad } from './$types';
import type { ToruData } from '$components/experiments/toru';
-export const load = (async ({ fetch }) => {
- let nowPlayingData: ToruData | undefined = undefined;
+export const load = (({ fetch }) => {
+ const nowPlayingData = fetch(`${TORU_API_URL}/kiosion?res=json&cover_size=medium`)
+ .then((res) => {
+ if (!res.ok) {
+ throw new Error('Failed to fetch now playing data');
+ }
- try {
- const toruRes = await fetch(
- 'https://toru.kio.dev/api/v1/kiosion?res=json&cover_size=large'
- );
+ return res
+ .json()
+ .then((data) => data.data)
+ .catch((e) => {
+ throw new Error('Failed to parse now playing data', e);
+ });
+ })
+ .catch((e) => {
+ Logger.error(e);
- if (toruRes.ok) {
- nowPlayingData = await toruRes.json().then((data) => data.data);
- }
- // eslint-disable-next-line no-empty
- } catch {}
+ return undefined;
+ }) satisfies Promise;
return { fetch, nowPlayingData };
}) satisfies PageLoad;
diff --git a/svelte-app/types/app/index.ts b/svelte-app/types/app/index.ts
index 69855c055..f888b9deb 100644
--- a/svelte-app/types/app/index.ts
+++ b/svelte-app/types/app/index.ts
@@ -23,31 +23,8 @@ declare global {
}
}
-export interface MenuStateOpt {
- disabled?: boolean;
- icon?: string;
- text?: string;
- action?: () => void | Promise | undefined;
-}
-
-export interface MenuState {
- open: boolean;
- pos: {
- x: number;
- y: number;
- };
- target: HTMLElement;
- opts: MenuStateOpt[];
-}
-
-// Some internal Sveltekit types
export type RouteFetch = (info: RequestInfo, init?: RequestInit) => Promise;
-export type Subscriber = (value: T) => void;
-export type Unsubscriber = () => void;
-export type Updater = (value: T) => T;
-export type Invalidator = (value?: T) => void;
-
// Re-exports
export * from '$types/api';
export * from '$types/config';