-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
indexeddb.ts
66 lines (59 loc) · 1.75 KB
/
indexeddb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const LEGACY_DATABASE_ID = 'signal';
export async function doesDatabaseExist(): Promise<boolean> {
window.SignalContext.log.info(
'Checking for the existence of IndexedDB data...'
);
return new Promise((resolve, reject) => {
const req = window.indexedDB.open(LEGACY_DATABASE_ID);
let existed = true;
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);
};
req.onsuccess = () => {
clearTimer();
req.result.close();
resolve(existed);
};
req.onupgradeneeded = () => {
if (req.result.version === 1) {
existed = false;
window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
}
};
});
}
export function removeDatabase(): Promise<void> {
return new Promise<void>((resolve, reject) => {
window.SignalContext.log.info(
`removeDatabase: Deleting IndexedDB database '${LEGACY_DATABASE_ID}'`
);
const request = window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
request.onerror = () => {
window.SignalContext.log.error(
'removeDatabase: Error deleting database.'
);
reject(new Error('Error deleting database'));
};
request.onsuccess = () => {
window.SignalContext.log.info(
'removeDatabase: Database deleted successfully'
);
resolve();
};
});
}