1- export default class DbHandler {
1+ export default class DbHandler < StoredValue = unknown > {
22 private idb : IDBFactory ;
33 private db ! : IDBDatabase ;
44
55 public constructor ( ) {
6- if ( ! indexedDB ) {
6+ if ( ! globalThis . indexedDB ) {
77 throw new Error ( "Your browser doesn't support a stable version of IndexedDB. Josh is unable to run without one." ) ;
88 }
99
10- this . idb = indexedDB ;
10+ this . idb = globalThis . indexedDB ;
1111 }
1212
1313 public init ( ) {
@@ -31,7 +31,7 @@ export default class DbHandler {
3131 } ) ;
3232 }
3333
34- public async set ( key : string , value : unknown ) {
34+ public async set < Value = StoredValue > ( key : string , value : Value ) {
3535 const all = this . open ( ) ;
3636 const doc = {
3737 key,
@@ -43,35 +43,34 @@ export default class DbHandler {
4343 await this . handleEvents ( request ) ;
4444 }
4545
46- public async get ( key : string ) {
46+ public async get < Value = StoredValue > ( key : string ) : Promise < Value | undefined > {
4747 const all = this . open ( ) ;
4848 const request = all . get ( key ) ;
49- const result = await this . handleEvents ( request ) ;
49+ const result = ( await this . handleEvents ( request ) ) as {
50+ value : Value | undefined ; // Its shit like this why I don't like TS
51+ } ;
5052
51- // @ts -ignore it exists f you TS
5253 return result ?. value ;
5354 }
5455
55- public async getAll ( ) {
56+ public async getAll < Value = StoredValue > ( ) : Promise < { [ key : string ] : Value } > {
5657 const all = this . open ( ) ;
5758 const request = all . getAll ( ) ;
58- const docs = await this . handleEvents ( request ) ;
59- const final = { } ;
59+ const docs = ( await this . handleEvents ( request ) ) as { key : string ; value : Value } [ ] ;
60+ const final : { [ key : string ] : Value } = { } ; // Why can't this be inferred from usage????
6061
61- // @ts -ignore TS GO AWAY
6262 docs . forEach ( ( x ) => {
63- // @ts -ignore TS GO AWAY
6463 final [ x . key ] = x . value ;
6564 } ) ;
6665
6766 return final ;
6867 }
6968
70- public async getKeys ( ) {
69+ public async getKeys ( ) : Promise < string [ ] > {
7170 const all = this . open ( ) ;
7271 const request = all . getAllKeys ( ) ;
7372
74- return this . handleEvents ( request ) ;
73+ return ( await this . handleEvents ( request ) ) as string [ ] ;
7574 }
7675
7776 public async count ( ) {
0 commit comments