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