From 1c1546184ca123ba600d73c0d1a4fca1d37dcf76 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Sun, 22 Jun 2025 16:45:49 +0200 Subject: [PATCH] refactor: output applets --- package.json | 1 - .../blur/artwork-controller/_applet.astro | 7 ++- src/pages/output/indexed-db/_applet.astro | 30 ++++------ src/pages/output/native-fs/_applet.astro | 47 ++++------------ src/scripts/output/indexed-db/constants.ts | 1 + src/scripts/output/indexed-db/worker.ts | 40 ++++++++++++++ src/scripts/output/native-fs/constants.ts | 2 + src/scripts/output/native-fs/worker.ts | 55 +++++++++++++++++++ 8 files changed, 126 insertions(+), 57 deletions(-) create mode 100644 src/scripts/output/indexed-db/constants.ts create mode 100644 src/scripts/output/indexed-db/worker.ts create mode 100644 src/scripts/output/native-fs/constants.ts create mode 100644 src/scripts/output/native-fs/worker.ts diff --git a/package.json b/package.json index dcb5f0c..f52b91c 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "iconoir": "^7.11.0", "idb-keyval": "^6.2.1", "music-metadata": "^11.2.3", - "native-file-system-adapter": "^3.0.1", "query-string": "^9.1.2", "spellcaster": "^6.0.0", "subsonic-api": "^3.1.2", diff --git a/src/pages/constituent/blur/artwork-controller/_applet.astro b/src/pages/constituent/blur/artwork-controller/_applet.astro index 2b15691..4ff079f 100644 --- a/src/pages/constituent/blur/artwork-controller/_applet.astro +++ b/src/pages/constituent/blur/artwork-controller/_applet.astro @@ -344,10 +344,15 @@ import "@styles/diffuse/fonts.css"; }; const _orchestrator = { - inputCache: await applet("../../../orchestrator/input-cache"), queueAudio: await applet("../../../orchestrator/queue-audio", { groupId: groupId(), }), + + // When using the `main` group, load additional orchestrators: + inputCache: + context.groupId === undefined || context.groupId === "main" + ? await applet("../../../orchestrator/input-cache") + : undefined, queueTracks: context.groupId === undefined || context.groupId === "main" ? applet("../../../orchestrator/queue-tracks") diff --git a/src/pages/output/indexed-db/_applet.astro b/src/pages/output/indexed-db/_applet.astro index 98a1c72..302173f 100644 --- a/src/pages/output/indexed-db/_applet.astro +++ b/src/pages/output/indexed-db/_applet.astro @@ -1,17 +1,21 @@ diff --git a/src/pages/output/native-fs/_applet.astro b/src/pages/output/native-fs/_applet.astro index 8cf3ecb..f38e1c6 100644 --- a/src/pages/output/native-fs/_applet.astro +++ b/src/pages/output/native-fs/_applet.astro @@ -1,19 +1,25 @@ diff --git a/src/scripts/output/indexed-db/constants.ts b/src/scripts/output/indexed-db/constants.ts new file mode 100644 index 0000000..34832a8 --- /dev/null +++ b/src/scripts/output/indexed-db/constants.ts @@ -0,0 +1 @@ +export const IDB_PREFIX = "@applets/output/indexed-db"; diff --git a/src/scripts/output/indexed-db/worker.ts b/src/scripts/output/indexed-db/worker.ts new file mode 100644 index 0000000..4ed4419 --- /dev/null +++ b/src/scripts/output/indexed-db/worker.ts @@ -0,0 +1,40 @@ +import * as IDB from "idb-keyval"; + +import { expose, jsonDecode, jsonEncode } from "@scripts/common"; +import type { Track } from "@applets/core/types"; +import { IDB_PREFIX } from "./constants"; + +//////////////////////////////////////////// +// ACTIONS +//////////////////////////////////////////// +const actions = expose({ + getTracks, + putTracks, +}); + +export type Actions = typeof actions; + +// Actions + +async function getTracks() { + const encoded = await get({ name: "tracks.json" }); + if (!encoded) return []; + return jsonDecode(encoded); +} + +async function putTracks(tracks: Track[]) { + const data = jsonEncode(tracks); + await put({ name: "tracks.json", data }); +} + +//////////////////////////////////////////// +// 🛠️ +//////////////////////////////////////////// + +async function get({ name }: { name: string }) { + return await IDB.get(`${IDB_PREFIX}/${name}`); +} + +async function put({ data, name }: { data: Uint8Array; name: string }) { + return await IDB.set(`${IDB_PREFIX}/${name}`, data); +} diff --git a/src/scripts/output/native-fs/constants.ts b/src/scripts/output/native-fs/constants.ts new file mode 100644 index 0000000..8a51698 --- /dev/null +++ b/src/scripts/output/native-fs/constants.ts @@ -0,0 +1,2 @@ +export const IDB_PREFIX = "@applets/output/native-fs"; +export const IDB_DEVICE_KEY = `${IDB_PREFIX}/device`; diff --git a/src/scripts/output/native-fs/worker.ts b/src/scripts/output/native-fs/worker.ts new file mode 100644 index 0000000..7551c9f --- /dev/null +++ b/src/scripts/output/native-fs/worker.ts @@ -0,0 +1,55 @@ +import * as IDB from "idb-keyval"; + +import { expose, jsonDecode, jsonEncode } from "@scripts/common"; +import type { Track } from "@applets/core/types"; +import { IDB_DEVICE_KEY, IDB_PREFIX } from "./constants"; + +//////////////////////////////////////////// +// ACTIONS +//////////////////////////////////////////// +const actions = expose({ + getTracks, + putTracks, +}); + +export type Actions = typeof actions; + +// Actions + +async function getTracks() { + const encoded = await get({ name: "tracks.json" }); + if (!encoded) return []; + return jsonDecode(encoded); +} + +async function putTracks(tracks: Track[]) { + const data = jsonEncode(tracks); + await put({ name: "tracks.json", data }); +} + +//////////////////////////////////////////// +// 🛠️ +//////////////////////////////////////////// + +async function get({ name }: { name: string }) { + const handle: FileSystemDirectoryHandle | null = (await IDB.get(IDB_DEVICE_KEY)) ?? null; + if (!handle) throw new Error("Storage not configured properly, handle not found."); + + try { + const fileHandle = await handle.getFileHandle(name); + const file = await fileHandle.getFile(); + const data = await file.bytes(); + return data; + } catch (err) { + return undefined; + } +} + +async function put({ data, name }: { data: Uint8Array; name: string }) { + const handle: FileSystemDirectoryHandle | null = (await IDB.get(IDB_DEVICE_KEY)) ?? null; + if (!handle) throw new Error("Storage not configured properly, handle not found."); + const fileHandle = await handle.getFileHandle(name, { create: true }); + const stream = await fileHandle.createWritable(); + await stream.write(data); + await stream.close(); +} -- 2.51.2