Skip to content

Delay opening a database connection until a request is made #1919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions packages/installations/src/helpers/idb-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ const DATABASE_NAME = 'firebase-installations-database';
const DATABASE_VERSION = 1;
const OBJECT_STORE_NAME = 'firebase-installations-store';

const dbPromise: Promise<DB> = openDb(
DATABASE_NAME,
DATABASE_VERSION,
upgradeDB => {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
let dbPromise: Promise<DB> | null = null;
function getDbPromise(): Promise<DB> {
if (!dbPromise) {
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
});
}
);
return dbPromise;
}

/** Gets record(s) from the objectStore that match the given key. */
export async function get(appConfig: AppConfig): Promise<unknown> {
const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
return db
.transaction(OBJECT_STORE_NAME)
.objectStore(OBJECT_STORE_NAME)
Expand All @@ -54,7 +56,7 @@ export async function set<ValueType>(
value: ValueType
): Promise<ValueType> {
const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
await tx.objectStore(OBJECT_STORE_NAME).put(value, key);
await tx.complete;
Expand All @@ -64,7 +66,7 @@ export async function set<ValueType>(
/** Removes record(s) from the objectStore that match the given key. */
export async function remove(appConfig: AppConfig): Promise<void> {
const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
await tx.complete;
Expand All @@ -81,7 +83,7 @@ export async function update<OldType, NewType>(
updateFn: (previousValue: OldType | undefined) => NewType
): Promise<NewType> {
const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
const store = tx.objectStore(OBJECT_STORE_NAME);
const oldValue = await store.get(key);
Expand All @@ -102,7 +104,7 @@ export async function update<OldType, NewType>(
}

export async function clear(): Promise<void> {
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
await tx.objectStore(OBJECT_STORE_NAME).clear();
await tx.complete;
Expand Down