a simple web player for subsonic tinysub.devins.page
subsonic navidrome javascript
9
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: extract db logic from queue-storage

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
becba232 08ff0464

+64 -28
+63
src/js/db.js
··· 1 + class Db { 2 + constructor() { 3 + this.db = null; 4 + this.dbName = "tinysub"; 5 + this.dbVersion = 3; 6 + } 7 + 8 + async open() { 9 + if (this.db) { 10 + return this.db; 11 + } 12 + 13 + const db = await this._openDatabase(); 14 + return db; 15 + } 16 + 17 + async _openDatabase() { 18 + try { 19 + return await new Promise((resolve, reject) => { 20 + const req = indexedDB.open(this.dbName, this.dbVersion); 21 + 22 + req.onsuccess = () => { 23 + this.db = req.result; 24 + resolve(this.db); 25 + }; 26 + 27 + req.onerror = () => reject(req.error); 28 + 29 + req.onupgradeneeded = (e) => { 30 + const db = e.target.result; 31 + const oldVersion = e.oldVersion; 32 + 33 + // 1 -> 2: create queueStorage 34 + if (oldVersion < 2 && !db.objectStoreNames.contains("queueStorage")) { 35 + db.createObjectStore("queueStorage"); 36 + } 37 + 38 + // 2 -> 3: create apiCache and cacheMetadata 39 + if (oldVersion < 3) { 40 + if (!db.objectStoreNames.contains("apiCache")) { 41 + db.createObjectStore("apiCache"); 42 + } 43 + if (!db.objectStoreNames.contains("cacheMetadata")) { 44 + db.createObjectStore("cacheMetadata"); 45 + } 46 + } 47 + }; 48 + }); 49 + } catch (err) { 50 + console.error("[Db] Failed to open IndexedDB:", err); 51 + return null; 52 + } 53 + } 54 + 55 + close() { 56 + if (this.db) { 57 + this.db.close(); 58 + this.db = null; 59 + } 60 + } 61 + } 62 + 63 + const db = new Db();
+1 -28
src/js/queue-storage.js
··· 1 1 // queue storage that persists to IndexedDB across sessions 2 2 3 3 class QueueStorage { 4 - constructor() { 5 - this.db = null; 6 - } 7 - 8 4 async ensureDB() { 9 - if (this.db) return this.db; 10 - 11 - try { 12 - return await new Promise((resolve) => { 13 - const req = indexedDB.open("tinysub", 2); 14 - 15 - req.onsuccess = () => { 16 - this.db = req.result; 17 - resolve(this.db); 18 - }; 19 - 20 - req.onupgradeneeded = (e) => { 21 - const db = e.target.result; 22 - if (!db.objectStoreNames.contains("queueStorage")) { 23 - db.createObjectStore("queueStorage"); 24 - } 25 - }; 26 - 27 - req.onerror = () => resolve(null); 28 - }); 29 - } catch (error) { 30 - console.error("[QueueStorage] IndexedDB initialization failed:", error); 31 - return null; 32 - } 5 + return await db.open(); 33 6 } 34 7 async save(songs, queueIndex) { 35 8 const db = await this.ensureDB();