Add a timeout to clearDatabase

This will trigger in case something goes wrong and a database isn't closed.
This commit is contained in:
toasted-nutbread 2020-02-22 13:09:18 -05:00
parent a54f44122a
commit 7b1a1480dc

View File

@ -107,7 +107,13 @@ function countKanjiWithCharacter(kanji, character) {
}
async function clearDatabase() {
function clearDatabase(timeout) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error(`clearDatabase failed to resolve after ${timeout}ms`));
}, timeout);
(async () => {
const indexedDB = global.indexedDB;
for (const {name} of await indexedDB.databases()) {
await new Promise((resolve, reject) => {
@ -116,6 +122,10 @@ async function clearDatabase() {
request.onsuccess = () => resolve();
});
}
clearTimeout(timer);
resolve();
})();
});
}
@ -840,11 +850,18 @@ async function testDatabase2() {
async function main() {
const clearTimeout = 5000;
try {
await testDatabase1();
await clearDatabase();
await clearDatabase(clearTimeout);
await testDatabase2();
await clearDatabase();
await clearDatabase(clearTimeout);
} catch (e) {
console.log(e);
process.exit(-1);
throw e;
}
}