I have the following snippet of code:
function open_db(dbname, dbversion, upgrade, onblocked) {
if (upgrade === undefined) {
upgrade = function basic_init(ev) {
…
};
}
if (onblocked === undefined) {
onblocked = function onblocked(ev) {
throw ev;
};
}
let req = window.indexedDB.open(dbname, dbversion);
return new Promise((resolve, reject) => {
req.onsuccess = ev => resolve(ev.target.result);
req.onerror = ev => reject(ev.target.error);
req.onupgradeneeded = ev => {
try {
return upgrade(ev);
} catch (error) {
reject(error);
ev.target.onsuccess = ev => ev.target.close(); // IS THIS LINE NECESSARY?
throw error; // IS THIS LINE UNNECESSARY?
}
};
req.onblocked = ev => {
try {
return onblocked(ev);
} catch (error) {
reject(error);
ev.target.onsuccess = ev => ev.target.close(); // IS THIS LINE NECESSARY?
throw error; // IS THIS LINE UNNECESSARY?
}
};
});
}
If the .onblocked
or .onupgradeneeded
handlers throw
a native error, will that cancel the open attempt? Or will the IDBOpenDBRequest
object ignore such errors and steam on ahead obliviously until I manually close the db if/after it's opened?
In a nutshell: are the commented lines of code necessary? Are they sufficient to prevent a dangling open handle?
Is there a better way to cancel the request-to-open, rather than just adding .onsuccess = ev => … .close()
?