Skip to content

Commit 19c3842

Browse files
authored
Delay opening a database connection until a request is made (#1919)
Prevents errors during page load on browsers that don't support IndexedDB.
1 parent 99ba995 commit 19c3842

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

packages/installations/src/helpers/idb-manager.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,28 @@ const DATABASE_NAME = 'firebase-installations-database';
2222
const DATABASE_VERSION = 1;
2323
const OBJECT_STORE_NAME = 'firebase-installations-store';
2424

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+
});
3839
}
39-
);
40+
return dbPromise;
41+
}
4042

4143
/** Gets record(s) from the objectStore that match the given key. */
4244
export async function get(appConfig: AppConfig): Promise<unknown> {
4345
const key = getKey(appConfig);
44-
const db = await dbPromise;
46+
const db = await getDbPromise();
4547
return db
4648
.transaction(OBJECT_STORE_NAME)
4749
.objectStore(OBJECT_STORE_NAME)
@@ -54,7 +56,7 @@ export async function set<ValueType>(
5456
value: ValueType
5557
): Promise<ValueType> {
5658
const key = getKey(appConfig);
57-
const db = await dbPromise;
59+
const db = await getDbPromise();
5860
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
5961
await tx.objectStore(OBJECT_STORE_NAME).put(value, key);
6062
await tx.complete;
@@ -64,7 +66,7 @@ export async function set<ValueType>(
6466
/** Removes record(s) from the objectStore that match the given key. */
6567
export async function remove(appConfig: AppConfig): Promise<void> {
6668
const key = getKey(appConfig);
67-
const db = await dbPromise;
69+
const db = await getDbPromise();
6870
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
6971
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
7072
await tx.complete;
@@ -81,7 +83,7 @@ export async function update<OldType, NewType>(
8183
updateFn: (previousValue: OldType | undefined) => NewType
8284
): Promise<NewType> {
8385
const key = getKey(appConfig);
84-
const db = await dbPromise;
86+
const db = await getDbPromise();
8587
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
8688
const store = tx.objectStore(OBJECT_STORE_NAME);
8789
const oldValue = await store.get(key);
@@ -102,7 +104,7 @@ export async function update<OldType, NewType>(
102104
}
103105

104106
export async function clear(): Promise<void> {
105-
const db = await dbPromise;
107+
const db = await getDbPromise();
106108
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
107109
await tx.objectStore(OBJECT_STORE_NAME).clear();
108110
await tx.complete;

0 commit comments

Comments
 (0)