forked from
tokono.ma/diffuse
A music player that connects to your cloud/distributed storage.
1import * as IDB from "idb-keyval";
2
3import { IDB_PREFIX } from "./constants.js";
4import { BroadcastedOutputElement, outputManager } from "../../common.js";
5import { defineElement } from "~/common/element.js";
6
7/**
8 * @import {OutputElement, OutputManager, OutputWorkerActions} from "../../types.d.ts"
9 * @import {SupportedDataTypes} from "./types.d.ts"
10 */
11
12////////////////////////////////////////////
13// ELEMENT
14////////////////////////////////////////////
15
16/**
17 * @implements {OutputElement<SupportedDataTypes>}
18 */
19class IndexedDBOutput extends BroadcastedOutputElement {
20 static NAME = "diffuse/output/polymorphic/indexed-db";
21 static WORKER_URL = "components/output/polymorphic/indexed-db/worker.js";
22
23 #manager;
24
25 constructor() {
26 super();
27
28 /** @type {OutputManager<SupportedDataTypes>} */
29 this.#manager = outputManager({
30 facets: {
31 empty: () => undefined,
32 get: () => this.#get("facets"),
33 put: (data) => this.#put("facets", data),
34 },
35 init: () => this.whenConnected(),
36 playlistItems: {
37 empty: () => undefined,
38 get: () => this.#get("playlistItems"),
39 put: (data) => this.#put("playlistItems", data),
40 },
41 settings: {
42 empty: () => undefined,
43 get: () => this.#get("settings"),
44 put: (data) => this.#put("settings", data),
45 },
46 tracks: {
47 empty: () => undefined,
48 get: () => this.#get("tracks"),
49 put: (data) => this.#put("tracks", data),
50 },
51 });
52
53 this.facets = this.#manager.facets;
54 this.playlistItems = this.#manager.playlistItems;
55 this.settings = this.#manager.settings;
56 this.tracks = this.#manager.tracks;
57
58 this.ready = () => true;
59 }
60
61 // LIFECYCLE
62
63 /** @override */
64 connectedCallback() {
65 this.replicateSavedData(this.#manager);
66 super.connectedCallback();
67 }
68
69 // GET & PUT
70
71 /** @param {string} name */
72 #get = (name) => IDB.get(`${IDB_PREFIX}/${this.#cat(name)}`);
73
74 /** @param {string} name; @param {any} data */
75 #put = (name, data) => IDB.set(`${IDB_PREFIX}/${this.#cat(name)}`, data);
76
77 // 🛠️
78
79 /** @param {string} name */
80 #cat(name) {
81 return `${this.namespace?.length ? this.namespace + "/" : ""}${name}`;
82 }
83}
84
85export default IndexedDBOutput;
86
87////////////////////////////////////////////
88// REGISTER
89////////////////////////////////////////////
90
91export const CLASS = IndexedDBOutput;
92export const NAME = "dop-indexed-db";
93
94defineElement(NAME, IndexedDBOutput);