Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. diffuse.sh
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import { BroadcastableDiffuseElement, query } from "~/common/element.js";import { computed, signal } from "~/common/signal.js";
/** * @import { OutputElement, OutputManagerDeputy } from "@specs/components/output/types.d.ts" */
/** * @template [T=null] */export class OutputTransformer extends BroadcastableDiffuseElement { static observedAttributes = ["output-selector"];
// SIGNALS
#output = signal(/** @type {OutputElement<T> | undefined} */ (undefined)); #outputWhenDefined = Promise.withResolvers();
output = { whenDefined: this.#outputWhenDefined.promise, signal: this.#output.get, };
// LIFECYCLE
/** * @override */ connectedCallback() { super.connectedCallback();
/** @type {OutputElement<T>} */ const output = query(this, "output-selector");
// When defined customElements.whenDefined(output.localName).then(() => { this.#output.value = output; this.#outputWhenDefined.resolve(null); }); }
/** * @override * @param {string} name * @param {string} oldValue * @param {string} newValue */ attributeChangedCallback(name, oldValue, newValue) { super.attributeChangedCallback(name, oldValue, newValue);
// Re-resolve the output when rewired at runtime (eg. when a transformer // is inserted into an existing output chain). Before connecting, the // initial query is handled by `connectedCallback`. if ( name === "output-selector" && this.isConnected && oldValue !== newValue ) { /** @type {OutputElement<T>} */ const output = query(this, "output-selector");
customElements.whenDefined(output.localName).then(() => { this.#output.value = output; this.#outputWhenDefined.resolve(null); }); } }
// MANAGER
base() { /** @type {OutputManagerDeputy<T>} */ const m = { collaborations: { collection: computed(() => { return this.output.signal()?.collaborations?.collection() ?? { state: "loading" }; }), reload: () => { return this.output.signal()?.collaborations?.reload() ?? Promise.resolve(); }, save: async (newCollaborations) => { await this.output.whenDefined; await this.output.signal()?.collaborations.save(newCollaborations); }, }, facets: { collection: computed(() => { return this.output.signal()?.facets?.collection() ?? { state: "loading" }; }), reload: () => { return this.output.signal()?.facets?.reload() ?? Promise.resolve(); }, save: async (newFacets) => { await this.output.whenDefined; await this.output.signal()?.facets.save(newFacets); }, }, playlistItems: { collection: computed(() => { return this.output.signal()?.playlistItems?.collection() ?? { state: "loading" }; }), reload: () => { return this.output.signal()?.playlistItems?.reload() ?? Promise.resolve(); }, save: async (newPlaylistItems) => { await this.output.whenDefined; await this.output.signal()?.playlistItems.save(newPlaylistItems); }, }, settings: { collection: computed(() => { return this.output.signal()?.settings?.collection() ?? { state: "loading" }; }), reload: () => { return this.output.signal()?.settings?.reload() ?? Promise.resolve(); }, save: async (newSettings) => { await this.output.whenDefined; await this.output.signal()?.settings.save(newSettings); }, }, tracks: { collection: computed(() => { return this.output.signal()?.tracks?.collection() ?? { state: "loading" }; }), reload: () => { return this.output.signal()?.tracks?.reload() ?? Promise.resolve(); }, save: async (newTracks) => { await this.output.whenDefined; await this.output.signal()?.tracks.save(newTracks); }, },
// Other non-data related state ready: computed(() => this.output.signal()?.ready() ?? false), };
return m; }}