11class Storage {
22 constructor ( id ) {
33 this . id = id ;
4+ this . _cache = null ;
5+ this . _idbName = id ;
6+ this . _idbVersion = 1 ;
7+ this . idbReady = this . _initIDB ( ) ;
48 this . syncWithCookie ( ) ;
59 }
610
711 set ( key , value ) {
12+ if ( key === 'cache' ) {
13+ try {
14+ this . _cache = value ;
15+ this . idbSet ( 'cache' , value ) . catch ( ( e ) => console . error ( 'IDB set failed' , e ) ) ;
16+ const temp = this . object ;
17+ if ( temp && temp . cache ) delete temp . cache ;
18+ localStorage . setItem ( this . id , JSON . stringify ( temp ) ) ;
19+ this . syncWithCookie ( ) ;
20+ } catch ( e ) {
21+ console . error ( 'storage.set(cache) failed' , e ) ;
22+ }
23+ return this ;
24+ }
25+
826 let temp = this . object ;
927 temp [ key ] = value ;
1028 localStorage . setItem ( this . id , JSON . stringify ( temp ) ) ;
@@ -13,6 +31,7 @@ class Storage {
1331 }
1432
1533 get ( key ) {
34+ if ( key === 'cache' ) return this . _cache ;
1635 return this . object [ key ] ;
1736 }
1837
@@ -21,6 +40,16 @@ class Storage {
2140 }
2241
2342 delete ( key ) {
43+ if ( key === 'cache' ) {
44+ this . _cache = null ;
45+ this . idbDelete ( 'cache' ) . catch ( ( e ) => console . error ( 'IDB delete failed' , e ) ) ;
46+ const temp = this . object ;
47+ if ( temp && temp . cache ) delete temp . cache ;
48+ localStorage . setItem ( this . id , JSON . stringify ( temp ) ) ;
49+ this . syncWithCookie ( ) ;
50+ return this ;
51+ }
52+
2453 let temp = this . object ;
2554 delete temp [ key ] ;
2655 localStorage . setItem ( this . id , JSON . stringify ( temp ) ) ;
@@ -31,6 +60,13 @@ class Storage {
3160 obliterate ( ) {
3261 localStorage . removeItem ( this . id ) ;
3362 setCookie ( this . id , "" , - 1 ) ;
63+ try {
64+ const req = indexedDB . deleteDatabase ( this . _idbName ) ;
65+ req . onsuccess = ( ) => { } ;
66+ req . onerror = ( ) => { console . error ( 'Failed to delete IDB' , req . error ) ; } ;
67+ } catch ( e ) {
68+ console . error ( 'obliterate IDB error' , e ) ;
69+ }
3470 }
3571
3672 get object ( ) {
@@ -46,6 +82,89 @@ class Storage {
4682 localStorage . setItem ( this . id , cookieData ) ;
4783 }
4884 }
85+
86+ _initIDB ( ) {
87+ return this . _openIDB ( )
88+ . then ( ( db ) => {
89+ this . _db = db ;
90+ return this . idbGet ( 'cache' ) . then ( ( v ) => { this . _cache = v ; } ) . catch ( ( ) => { this . _cache = null ; } ) ;
91+ } )
92+ . catch ( ( e ) => {
93+ console . error ( 'IndexedDB unavailable' , e ) ;
94+ } ) ;
95+ }
96+
97+ _openIDB ( ) {
98+ return new Promise ( ( resolve , reject ) => {
99+ if ( ! window . indexedDB ) return reject ( new Error ( 'IndexedDB not supported' ) ) ;
100+ const request = indexedDB . open ( this . _idbName , this . _idbVersion ) ;
101+ request . onupgradeneeded = ( event ) => {
102+ const db = event . target . result ;
103+ if ( ! db . objectStoreNames . contains ( 'virtual-falcons' ) ) db . createObjectStore ( 'virtual-falcons' , { keyPath : 'key' } ) ;
104+ } ;
105+ request . onsuccess = ( ) => resolve ( request . result ) ;
106+ request . onerror = ( ) => reject ( request . error ) ;
107+ } ) ;
108+ }
109+
110+ idbSet ( key , value ) {
111+ return new Promise ( ( resolve , reject ) => {
112+ this . _openIDB ( ) . then ( ( db ) => {
113+ const tx = db . transaction ( [ 'virtual-falcons' ] , 'readwrite' ) ;
114+ const store = tx . objectStore ( 'virtual-falcons' ) ;
115+ const req = store . put ( { key : key , value : value } ) ;
116+ req . onsuccess = ( ) => resolve ( ) ;
117+ req . onerror = ( ) => reject ( req . error ) ;
118+ } ) . catch ( reject ) ;
119+ } ) ;
120+ }
121+
122+ idbGet ( key ) {
123+ return new Promise ( ( resolve , reject ) => {
124+ this . _openIDB ( ) . then ( ( db ) => {
125+ const tx = db . transaction ( [ 'virtual-falcons' ] , 'readonly' ) ;
126+ const store = tx . objectStore ( 'virtual-falcons' ) ;
127+ const req = store . get ( key ) ;
128+ req . onsuccess = ( ) => {
129+ if ( req . result ) resolve ( req . result . value ) ; else resolve ( undefined ) ;
130+ } ;
131+ req . onerror = ( ) => reject ( req . error ) ;
132+ } ) . catch ( reject ) ;
133+ } ) ;
134+ }
135+
136+ idbDelete ( key ) {
137+ return new Promise ( ( resolve , reject ) => {
138+ this . _openIDB ( ) . then ( ( db ) => {
139+ const tx = db . transaction ( [ 'virtual-falcons' ] , 'readwrite' ) ;
140+ const store = tx . objectStore ( 'virtual-falcons' ) ;
141+ const req = store . delete ( key ) ;
142+ req . onsuccess = ( ) => resolve ( ) ;
143+ req . onerror = ( ) => reject ( req . error ) ;
144+ } ) . catch ( reject ) ;
145+ } ) ;
146+ }
147+
148+ idbAll ( ) {
149+ return new Promise ( ( resolve , reject ) => {
150+ this . _openIDB ( ) . then ( ( db ) => {
151+ const tx = db . transaction ( [ 'virtual-falcons' ] , 'readonly' ) ;
152+ const store = tx . objectStore ( 'virtual-falcons' ) ;
153+ const req = store . openCursor ( ) ;
154+ const out = { } ;
155+ req . onsuccess = ( e ) => {
156+ const cursor = e . target . result ;
157+ if ( cursor ) {
158+ out [ cursor . key ] = cursor . value . value ;
159+ cursor . continue ( ) ;
160+ } else {
161+ resolve ( out ) ;
162+ }
163+ } ;
164+ req . onerror = ( ) => reject ( req . error ) ;
165+ } ) . catch ( reject ) ;
166+ } ) ;
167+ }
49168}
50169
51170function setCookie ( name , value , days ) {
0 commit comments