@@ -22,26 +22,28 @@ const DATABASE_NAME = 'firebase-installations-database';
22
22
const DATABASE_VERSION = 1 ;
23
23
const OBJECT_STORE_NAME = 'firebase-installations-store' ;
24
24
25
- const dbPromise : Promise < DB > = openDb (
26
- DATABASE_NAME ,
27
- DATABASE_VERSION ,
28
- upgradeDB => {
29
- // We don't use 'break' in this switch statement, the fall-through
30
- // behavior is what we want, because if there are multiple versions between
31
- // the old version and the current version, we want ALL the migrations
32
- // that correspond to those versions to run, not only the last one.
33
- // eslint-disable-next-line default-case
34
- switch ( upgradeDB . oldVersion ) {
35
- case 0 :
36
- upgradeDB . createObjectStore ( OBJECT_STORE_NAME ) ;
37
- }
25
+ let dbPromise : Promise < DB > | null = null ;
26
+ function getDbPromise ( ) : Promise < DB > {
27
+ if ( ! dbPromise ) {
28
+ dbPromise = openDb ( DATABASE_NAME , DATABASE_VERSION , upgradeDB => {
29
+ // We don't use 'break' in this switch statement, the fall-through
30
+ // behavior is what we want, because if there are multiple versions between
31
+ // the old version and the current version, we want ALL the migrations
32
+ // that correspond to those versions to run, not only the last one.
33
+ // eslint-disable-next-line default-case
34
+ switch ( upgradeDB . oldVersion ) {
35
+ case 0 :
36
+ upgradeDB . createObjectStore ( OBJECT_STORE_NAME ) ;
37
+ }
38
+ } ) ;
38
39
}
39
- ) ;
40
+ return dbPromise ;
41
+ }
40
42
41
43
/** Gets record(s) from the objectStore that match the given key. */
42
44
export async function get ( appConfig : AppConfig ) : Promise < unknown > {
43
45
const key = getKey ( appConfig ) ;
44
- const db = await dbPromise ;
46
+ const db = await getDbPromise ( ) ;
45
47
return db
46
48
. transaction ( OBJECT_STORE_NAME )
47
49
. objectStore ( OBJECT_STORE_NAME )
@@ -54,7 +56,7 @@ export async function set<ValueType>(
54
56
value : ValueType
55
57
) : Promise < ValueType > {
56
58
const key = getKey ( appConfig ) ;
57
- const db = await dbPromise ;
59
+ const db = await getDbPromise ( ) ;
58
60
const tx = db . transaction ( OBJECT_STORE_NAME , 'readwrite' ) ;
59
61
await tx . objectStore ( OBJECT_STORE_NAME ) . put ( value , key ) ;
60
62
await tx . complete ;
@@ -64,7 +66,7 @@ export async function set<ValueType>(
64
66
/** Removes record(s) from the objectStore that match the given key. */
65
67
export async function remove ( appConfig : AppConfig ) : Promise < void > {
66
68
const key = getKey ( appConfig ) ;
67
- const db = await dbPromise ;
69
+ const db = await getDbPromise ( ) ;
68
70
const tx = db . transaction ( OBJECT_STORE_NAME , 'readwrite' ) ;
69
71
await tx . objectStore ( OBJECT_STORE_NAME ) . delete ( key ) ;
70
72
await tx . complete ;
@@ -81,7 +83,7 @@ export async function update<OldType, NewType>(
81
83
updateFn : ( previousValue : OldType | undefined ) => NewType
82
84
) : Promise < NewType > {
83
85
const key = getKey ( appConfig ) ;
84
- const db = await dbPromise ;
86
+ const db = await getDbPromise ( ) ;
85
87
const tx = db . transaction ( OBJECT_STORE_NAME , 'readwrite' ) ;
86
88
const store = tx . objectStore ( OBJECT_STORE_NAME ) ;
87
89
const oldValue = await store . get ( key ) ;
@@ -102,7 +104,7 @@ export async function update<OldType, NewType>(
102
104
}
103
105
104
106
export async function clear ( ) : Promise < void > {
105
- const db = await dbPromise ;
107
+ const db = await getDbPromise ( ) ;
106
108
const tx = db . transaction ( OBJECT_STORE_NAME , 'readwrite' ) ;
107
109
await tx . objectStore ( OBJECT_STORE_NAME ) . clear ( ) ;
108
110
await tx . complete ;
0 commit comments