1- const DB_NAME = "HTMLPlayerDB" ;
2- const DB_VERSION = 2 ;
3- const STORE = "albumArt" ;
4-
5- const openDatabase = ( ) : Promise < IDBDatabase > => {
6- return new Promise ( ( resolve , reject ) => {
7- const request = indexedDB . open ( DB_NAME , DB_VERSION ) ;
8-
9- request . onerror = ( ) => reject ( request . error ) ;
10- request . onsuccess = ( ) => resolve ( request . result ) ;
11-
12- request . onupgradeneeded = ( event ) => {
13- const db = ( event . target as IDBOpenDBRequest ) . result ;
14- if ( ! db . objectStoreNames . contains ( STORE ) ) {
15- db . createObjectStore ( STORE , { keyPath : "songId" } ) ;
16- }
17- } ;
18- } ) ;
19- } ;
1+ import { getDb , STORES } from "./db" ;
202
213const albumArtCache = new Map < string , string > ( ) ;
224const MAX_CACHE = 50 ;
235
6+ function evictCache ( ) {
7+ if ( albumArtCache . size >= MAX_CACHE ) {
8+ const firstKey = albumArtCache . keys ( ) . next ( ) . value ;
9+ if ( firstKey ) albumArtCache . delete ( firstKey ) ;
10+ }
11+ }
12+
2413export const albumArtStorage = {
2514 async load ( songId : string ) : Promise < string | null > {
2615 if ( albumArtCache . has ( songId ) ) {
2716 return albumArtCache . get ( songId ) ! ;
2817 }
2918
3019 try {
31- const db = await openDatabase ( ) ;
32- const tx = db . transaction ( [ STORE ] , "readonly" ) ;
33- const store = tx . objectStore ( STORE ) ;
20+ const db = await getDb ( ) ;
21+ const tx = db . transaction ( STORES . ALBUM_ART , "readonly" ) ;
22+ const store = tx . objectStore ( STORES . ALBUM_ART ) ;
3423
35- const result = await new Promise <
36- { songId : string ; albumArt : string } | undefined
37- > ( ( resolve , reject ) => {
24+ const result = await new Promise < { songId : string ; albumArt : string } | undefined > ( ( resolve , reject ) => {
3825 const req = store . get ( songId ) ;
3926 req . onsuccess = ( ) => resolve ( req . result ) ;
4027 req . onerror = ( ) => reject ( req . error ) ;
4128 } ) ;
4229
43- db . close ( ) ;
44-
4530 if ( result ?. albumArt ) {
4631 evictCache ( ) ;
4732 albumArtCache . set ( songId , result . albumArt ) ;
@@ -70,25 +55,20 @@ export const albumArtStorage = {
7055 if ( toLoad . length === 0 ) return result ;
7156
7257 try {
73- const db = await openDatabase ( ) ;
74- const tx = db . transaction ( [ STORE ] , "readonly" ) ;
75- const store = tx . objectStore ( STORE ) ;
58+ const db = await getDb ( ) ;
59+ const tx = db . transaction ( STORES . ALBUM_ART , "readonly" ) ;
60+ const store = tx . objectStore ( STORES . ALBUM_ART ) ;
7661
7762 const loaded = await Promise . all (
78- toLoad . map (
79- ( songId ) =>
80- new Promise < { songId : string ; albumArt : string } | null > (
81- ( resolve , reject ) => {
82- const req = store . get ( songId ) ;
83- req . onsuccess = ( ) => resolve ( req . result || null ) ;
84- req . onerror = ( ) => reject ( req . error ) ;
85- } ,
86- ) ,
87- ) ,
63+ toLoad . map ( ( songId ) =>
64+ new Promise < { songId : string ; albumArt : string } | null > ( ( resolve , reject ) => {
65+ const req = store . get ( songId ) ;
66+ req . onsuccess = ( ) => resolve ( req . result || null ) ;
67+ req . onerror = ( ) => reject ( req . error ) ;
68+ } )
69+ )
8870 ) ;
8971
90- db . close ( ) ;
91-
9272 for ( const art of loaded ) {
9373 if ( art ?. albumArt ) {
9474 evictCache ( ) ;
@@ -106,17 +86,16 @@ export const albumArtStorage = {
10686
10787 async save ( songId : string , albumArt : string ) : Promise < void > {
10888 try {
109- const db = await openDatabase ( ) ;
110- const tx = db . transaction ( [ STORE ] , "readwrite" ) ;
111- const store = tx . objectStore ( STORE ) ;
89+ const db = await getDb ( ) ;
90+ const tx = db . transaction ( STORES . ALBUM_ART , "readwrite" ) ;
91+ const store = tx . objectStore ( STORES . ALBUM_ART ) ;
11292
11393 await new Promise < void > ( ( resolve , reject ) => {
11494 const req = store . put ( { songId, albumArt } ) ;
11595 req . onsuccess = ( ) => resolve ( ) ;
11696 req . onerror = ( ) => reject ( req . error ) ;
11797 } ) ;
11898
119- db . close ( ) ;
12099 evictCache ( ) ;
121100 albumArtCache . set ( songId , albumArt ) ;
122101 } catch ( error ) {
@@ -135,16 +114,4 @@ export const albumArtStorage = {
135114 get ( songId : string ) : string | undefined {
136115 return albumArtCache . get ( songId ) ;
137116 } ,
138-
139- set ( songId : string , albumArt : string ) : void {
140- evictCache ( ) ;
141- albumArtCache . set ( songId , albumArt ) ;
142- } ,
143117} ;
144-
145- function evictCache ( ) {
146- if ( albumArtCache . size >= MAX_CACHE ) {
147- const firstKey = albumArtCache . keys ( ) . next ( ) . value ;
148- if ( firstKey ) albumArtCache . delete ( firstKey ) ;
149- }
150- }
0 commit comments