diff --git a/src/components/configurator/output/element.js b/src/components/configurator/output/element.js new file mode 100644 index 00000000..70551c15 --- /dev/null +++ b/src/components/configurator/output/element.js @@ -0,0 +1,67 @@ +import { DiffuseElement } from "@common/element.js"; +import { computed, signal } from "@common/signal.js"; + +/** + * @import {ProxiedActions} from "@common/worker.d.ts" + * @import {Track} from "@definitions/types.d.ts" + * @import {OutputManager, OutputElement} from "@components/output/types.d.ts" + */ + +//////////////////////////////////////////// +// ELEMENT +//////////////////////////////////////////// + +/** + * @implements {OutputManager} + */ +class OutputConfigurator extends DiffuseElement { + static NAME = "diffuse/configurator/output"; + static WORKER_URL = "components/configurator/output/worker.js"; + + constructor() { + super(); + + /** @type {OutputManager} */ + const manager = { + tracks: { + collection: computed(() => { + return this.#memory.tracks.value; + }), + reload: async () => {}, + save: async (newTracks) => { + this.#memory.tracks.value = newTracks; + }, + state: () => "loaded", + }, + }; + + // Assign manager properties to class + this.tracks = manager.tracks; + } + + // SIGNALS + + #memory = { + tracks: signal(/** @type {Track[]} */ ([])), + }; + + // LIFECYCLE + + /** + * @override + */ + async connectedCallback() { + super.connectedCallback(); + } +} + +export default OutputConfigurator; + +//////////////////////////////////////////// +// REGISTER +//////////////////////////////////////////// + +export const CLASS = OutputConfigurator; +export const NAME = "dc-output"; + +customElements.define(NAME, CLASS); diff --git a/src/components/output/polymorphic/indexed-db/element.js b/src/components/output/polymorphic/indexed-db/element.js index 5c2c2128..e99e2b3e 100644 --- a/src/components/output/polymorphic/indexed-db/element.js +++ b/src/components/output/polymorphic/indexed-db/element.js @@ -4,6 +4,7 @@ import { outputManager } from "../../common.js"; /** * @import {ProxiedActions} from "@common/worker.d.ts" * @import {OutputManager, OutputWorkerActions} from "../../types.d.ts" + * @import {SupportedDataTypes} from "./types.d.ts" */ //////////////////////////////////////////// @@ -20,20 +21,26 @@ class IndexedDBOutput extends DiffuseElement { constructor() { super(); - /** @type {ProxiedActions} */ + /** @type {ProxiedActions>} */ const p = this.workerProxy(); - // Manager + /** @type {OutputManager} */ const manager = outputManager({ tracks: { - empty: () => [], - get: p.getTracks, - put: p.putTracks, + empty: () => undefined, + get: () => p.get({ name: this.#cat("tracks") }), + put: (data) => p.put({ name: this.#cat("tracks"), data }), }, }); this.tracks = manager.tracks; } + + /** @param {string} name */ + #cat(name) { + const key = this.hasAttribute("key") ? this.getAttribute("key") + "/" : ""; + return `${key}${name}`; + } } export default IndexedDBOutput; diff --git a/src/components/output/polymorphic/indexed-db/types.d.ts b/src/components/output/polymorphic/indexed-db/types.d.ts new file mode 100644 index 00000000..0c35ba7e --- /dev/null +++ b/src/components/output/polymorphic/indexed-db/types.d.ts @@ -0,0 +1 @@ +export type SupportedDataTypes = any; diff --git a/src/components/output/polymorphic/indexed-db/worker.js b/src/components/output/polymorphic/indexed-db/worker.js index e6b21440..2c2b0386 100644 --- a/src/components/output/polymorphic/indexed-db/worker.js +++ b/src/components/output/polymorphic/indexed-db/worker.js @@ -4,7 +4,8 @@ import { IDB_PREFIX } from "./constants.js"; import { ostiary, rpc } from "@common/worker.js"; /** - * @import {Track} from "@definitions/types.d.ts"; + * @import {OutputWorkerActions} from "@components/output/types.d.ts"; + * @import {SupportedDataTypes} from "./types.d.ts" */ //////////////////////////////////////////// @@ -12,46 +13,25 @@ import { ostiary, rpc } from "@common/worker.js"; //////////////////////////////////////////// /** - * @returns {Promise} + * @type {OutputWorkerActions["get"]} */ -export async function getTracks() { - /** @type {Track[] | null} */ - const tracks = await get({ name: "tracks.json" }); - return tracks ?? []; +export async function get({ name }) { + return await IDB.get(`${IDB_PREFIX}/${name}`); } /** - * @param {Track[]} tracks + * @type {OutputWorkerActions["put"]} */ -export async function putTracks(tracks) { - await put({ name: "tracks.json", data: tracks }); +export async function put({ data, name }) { + return await IDB.set(`${IDB_PREFIX}/${name}`, data); } - //////////////////////////////////////////// // ⚡️ //////////////////////////////////////////// ostiary((context) => { rpc(context, { - getTracks, - putTracks, + get, + put, }); }); - -//////////////////////////////////////////// -// ⛔️ -//////////////////////////////////////////// - -/** - * @param {{ name: string }} _ - */ -async function get({ name }) { - return await IDB.get(`${IDB_PREFIX}/${name}`); -} - -/** - * @param {{ data: any; name: string }} _ - */ -async function put({ data, name }) { - return await IDB.set(`${IDB_PREFIX}/${name}`, data); -} diff --git a/src/components/output/types.d.ts b/src/components/output/types.d.ts index d46bb8ae..5f7bf279 100644 --- a/src/components/output/types.d.ts +++ b/src/components/output/types.d.ts @@ -1,5 +1,4 @@ import type { SignalReader } from "@common/signal.d.ts"; -import type { Track } from "@definitions/types.d.ts"; import type { DiffuseElement } from "@common/element.js"; export type OutputElement = DiffuseElement & OutputManager; @@ -22,7 +21,7 @@ export type OutputManagerProperties = { }; }; -export type OutputWorkerActions = { - getTracks(): Promise; - putTracks(tracks: Track[]): Promise; +export type OutputWorkerActions = { + get(args: { name: string }): Promise; + put(args: { data: DataType; name: string }): Promise; }; diff --git a/src/components/transformer/output/base.js b/src/components/transformer/output/base.js new file mode 100644 index 00000000..9de69c6a --- /dev/null +++ b/src/components/transformer/output/base.js @@ -0,0 +1,65 @@ +import { DiffuseElement, query } from "@common/element.js"; +import { computed, signal } from "@common/signal.js"; + +/** + * @import { OutputElement, OutputManager } from "../../output/types.d.ts" + */ + +/** + * @template T + */ +export class OutputTransformer extends DiffuseElement { + // SIGNALS + + #output = signal(/** @type {OutputElement | undefined} */ (undefined)); + #outputWhenDefined = Promise.withResolvers(); + + output = { + whenDefined: this.#outputWhenDefined.promise, + signal: this.#output.get, + }; + + // LIFECYCLE + + /** + * @override + */ + connectedCallback() { + super.connectedCallback(); + + /** @type {OutputElement} */ + const output = query(this, "output-selector"); + + // When defined + customElements.whenDefined(output.localName).then(() => { + this.#output.value = output; + this.#outputWhenDefined.resolve(null); + }); + } + + // MANAGER + + base() { + /** @type {OutputManager} */ + const m = { + tracks: { + collection: computed(() => { + return this.output.signal()?.tracks?.collection(); + }), + reload: () => { + return this.output.signal()?.tracks?.reload() ?? Promise.resolve(); + }, + save: async (newTracks) => { + if (newTracks === undefined) return; + await this.output.whenDefined; + await this.output.signal()?.tracks.save(newTracks); + }, + state: computed(() => + this.output.signal()?.tracks.state() ?? "loading" + ), + }, + }; + + return m; + } +} diff --git a/src/components/transformer/output/refiner/default/element.js b/src/components/transformer/output/refiner/default/element.js index b15012b8..716dc941 100644 --- a/src/components/transformer/output/refiner/default/element.js +++ b/src/components/transformer/output/refiner/default/element.js @@ -1,64 +1,37 @@ -import { DiffuseElement, query } from "@common/element.js"; -import { computed, signal } from "@common/signal.js"; +import { computed } from "@common/signal.js"; +import { OutputTransformer } from "../../base.js"; /** - * @import { OutputElement, OutputManager } from "../../../../output/types.d.ts" + * @import { OutputManager } from "../../../../output/types.d.ts" * @import { Track } from "@definitions/types.d.ts" */ -class DefaultOutputRefinerTransformer extends DiffuseElement { +/** + * @extends {OutputTransformer} + */ +class DefaultOutputRefinerTransformer extends OutputTransformer { constructor() { super(); + const base = this.base(); + /** @type {OutputManager} */ const manager = { tracks: { + ...base.tracks, collection: computed(() => { - return this.#defined.value - ? this.output?.tracks?.collection() ?? [] - : []; + return base.tracks.collection() ?? []; }), - reload: () => this.output?.tracks?.reload() ?? Promise.resolve(), save: async (newTracks) => { const filtered = newTracks.filter((t) => !t.ephemeral); - - if (!this.output) return; - - await customElements.whenDefined(this.output.localName); - await this.output.tracks.save(filtered); + await base.tracks.save(filtered); }, - state: computed(() => this.output?.tracks.state() ?? "loading"), }, }; // Assign manager properties to class this.tracks = manager.tracks; } - - /** @type {OutputElement | undefined} */ - output = undefined; - - // SIGNALS - - #defined = signal(false); - - // LIFECYCLE - - /** - * @override - */ - connectedCallback() { - super.connectedCallback(); - - /** @type {OutputElement} */ - const output = query(this, "output-selector"); - this.output = output; - - // When defined - customElements.whenDefined(this.output.localName).then( - () => this.#defined.value = true, - ); - } } export default DefaultOutputRefinerTransformer; diff --git a/src/components/transformer/output/string/json/element.js b/src/components/transformer/output/string/json/element.js index 1356c7e5..7867d19b 100644 --- a/src/components/transformer/output/string/json/element.js +++ b/src/components/transformer/output/string/json/element.js @@ -1,26 +1,26 @@ -import { DiffuseElement, query } from "@common/element.js"; -import { computed, signal } from "@common/signal.js"; +import { computed } from "@common/signal.js"; +import { OutputTransformer } from "../../base.js"; /** - * @import { OutputElement, OutputManager } from "../../../../output/types.d.ts" + * @import { OutputManager } from "../../../../output/types.d.ts" * @import { Track } from "@definitions/types.d.ts" */ -class JsonStringOutputTransformer extends DiffuseElement { +/** + * @extends {OutputTransformer} + */ +class JsonStringOutputTransformer extends OutputTransformer { constructor() { super(); + const base = this.base(); + /** @type {OutputManager} */ const manager = { tracks: { + ...base.tracks, collection: computed(() => { - const json = this.#defined.value - ? this.output?.tracks?.collection() ?? [] - : []; - - // In addition to the above, Some polymorphic outputs - // use an empty array as the default return value. - if (Array.isArray(json)) return json; + const json = base.tracks.collection() ?? "[]"; // Try parsing JSON try { @@ -32,47 +32,16 @@ class JsonStringOutputTransformer extends DiffuseElement { return []; } }), - reload: () => this.output?.tracks?.reload() ?? Promise.resolve(), save: async (newTracks) => { const json = JSON.stringify(newTracks); - - if (!this.output) return; - - await customElements.whenDefined(this.output.localName); - await this.output.tracks.save(json); + await base.tracks.save(json); }, - state: computed(() => this.output?.tracks?.state() ?? "loading"), }, }; // Assign manager properties to class this.tracks = manager.tracks; } - - /** @type {OutputElement | undefined} */ - output = undefined; - - // SIGNALS - - #defined = signal(false); - - // LIFECYCLE - - /** - * @override - */ - connectedCallback() { - super.connectedCallback(); - - /** @type {OutputElement} */ - const output = query(this, "output-selector"); - this.output = output; - - // When defined - customElements.whenDefined(this.output.localName).then( - () => this.#defined.value = true, - ); - } } export default JsonStringOutputTransformer; diff --git a/src/themes/webamp/index.js b/src/themes/webamp/index.js index 1f906fa2..b154e1eb 100644 --- a/src/themes/webamp/index.js +++ b/src/themes/webamp/index.js @@ -1,4 +1,4 @@ -// import "@components/orchestrator/process-tracks/element.js"; +import "@components/orchestrator/process-tracks/element.js"; import "@components/orchestrator/queue-tracks/element.js"; import "@components/input/opensubsonic/element.js"; import "@components/input/s3/element.js";