File tree Expand file tree Collapse file tree 1 file changed +22
-4
lines changed Expand file tree Collapse file tree 1 file changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,15 @@ class Store {
9
9
idb : IDBDatabase | null = null ;
10
10
state : State = new State ( ) ;
11
11
12
+ ready : Promise < void > ;
13
+ private resolveReady ! : ( ) => void ;
14
+
15
+ constructor ( ) {
16
+ this . ready = new Promise < void > ( ( resolve ) => {
17
+ this . resolveReady = resolve ;
18
+ } ) ;
19
+ }
20
+
12
21
async init ( ) {
13
22
this . idb = await this . openIndexedBD ( ) ;
14
23
@@ -51,14 +60,23 @@ class Store {
51
60
. transaction ( OBJECT_STORE_NAME , "readonly" )
52
61
. objectStore ( OBJECT_STORE_NAME ) ;
53
62
63
+ let pending = 0 ;
64
+
54
65
for ( const [ ref , property ] of this . state ) {
66
+ pending ++ ;
67
+
55
68
objectStore . get ( property ) . onsuccess = ( event ) => {
56
69
const value : unknown = ( event . target as IDBRequest ) . result ;
57
70
if ( value !== undefined && typeof value === typeof ref . value ) {
58
71
ref . value = value as typeof ref . value ;
59
- return ;
72
+ } else {
73
+ this . put ( ref . value , property ) ;
74
+ }
75
+
76
+ pending -- ;
77
+ if ( pending === 0 ) {
78
+ this . resolveReady ( ) ;
60
79
}
61
- this . put ( ref . value , property ) ;
62
80
} ;
63
81
}
64
82
@@ -68,7 +86,7 @@ class Store {
68
86
}
69
87
}
70
88
71
- const store = new Store ( ) ;
72
- await store . init ( ) ;
89
+ export const store = new Store ( ) ;
90
+ void store . init ( ) ;
73
91
74
92
export const state = store . state ;
You can’t perform that action at this time.
0 commit comments