signal-desktop/ts/indexeddb.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-05-27 22:12:01 +00:00
// Copyright 2018-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2022-05-27 22:12:01 +00:00
const LEGACY_DATABASE_ID = 'signal';
2018-10-18 01:01:21 +00:00
2022-06-01 16:51:30 +00:00
export async function doesDatabaseExist(): Promise<boolean> {
window.SignalContext.log.info(
'Checking for the existence of IndexedDB data...'
);
2018-10-18 01:01:21 +00:00
return new Promise((resolve, reject) => {
2022-05-27 22:12:01 +00:00
const req = window.indexedDB.open(LEGACY_DATABASE_ID);
2018-10-18 01:01:21 +00:00
let existed = true;
2022-06-01 16:51:30 +00:00
let timer: undefined | ReturnType<typeof setTimeout> = setTimeout(() => {
window.SignalContext.log.warn(
'doesDatabaseExist: Timed out attempting to check IndexedDB status'
);
return resolve(false);
}, 1000);
const clearTimer = () => {
if (timer !== undefined) {
clearTimeout(timer);
timer = undefined;
}
};
req.onerror = error => {
clearTimer();
reject(error);
};
2018-10-18 01:01:21 +00:00
req.onsuccess = () => {
clearTimer();
2018-10-18 01:01:21 +00:00
req.result.close();
resolve(existed);
};
req.onupgradeneeded = () => {
if (req.result.version === 1) {
existed = false;
2022-05-27 22:12:01 +00:00
window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
2018-10-18 01:01:21 +00:00
}
};
});
}
2022-06-01 16:51:30 +00:00
export function removeDatabase(): void {
window.SignalContext.log.info(
2022-05-27 22:12:01 +00:00
`Deleting IndexedDB database '${LEGACY_DATABASE_ID}'`
);
2022-05-27 22:12:01 +00:00
window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
2018-10-18 01:01:21 +00:00
}