A music player that connects to your cloud/distributed storage. diffuse.sh
Something went wrong. Try again.
2.4 kB · 86 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import { queryOptional } from "~/common/element.js";import * as Output from "~/common/output.js";
/** * @import {DiffuseElement} from "~/common/element.js" * @import {OutputElement} from "@specs/components/output/types.d.ts" */
/** * Read a stored session value from output settings when an output is configured, * otherwise falls back to localStorage. * * @param {DiffuseElement} element * @param {string} key * @returns {Promise<string | null>} */export async function readSession(element, key) { /** @type {OutputElement | null} */ const output = queryOptional(element, "output-selector");
if (output) { const settings = await Output.data(output.settings); const existing = settings.find((s) => s.key === key); if (existing) return existing.value; }
return localStorage.getItem(key);}
/** * Save a session value to output settings when an output is configured, * otherwise falls back to localStorage. * * @param {DiffuseElement} element * @param {string} key * @param {string} value */export async function saveSession(element, key, value) { /** @type {OutputElement | null} */ const output = queryOptional(element, "output-selector");
if (output) { const settings = await Output.data(output.settings); const existing = settings.find((s) => s.key === key);
const updated = existing ? settings.map((s) => s.key === key ? { ...s, value } : s) : [ ...settings, { $type: /** @type {"sh.diffuse.output.setting"} */ ( "sh.diffuse.output.setting" ), id: crypto.randomUUID(), key, value, }, ];
await output.settings.save(updated); } else { localStorage.setItem(key, value); }}
/** * Remove a stored session value from output settings when an output is configured, * otherwise falls back to localStorage. * * @param {DiffuseElement} element * @param {string} key */export async function clearSession(element, key) { /** @type {OutputElement | null} */ const output = queryOptional(element, "output-selector");
if (output) { const settings = await Output.data(output.settings); const updated = settings.filter((s) => s.key !== key); if (updated.length !== settings.length) { await output.settings.save(updated); } } else { localStorage.removeItem(key); }}