Skip to content

Commit

Permalink
Merge pull request #807 from AdamStone/dropInstance-onblocked
Browse files Browse the repository at this point in the history
Avoid uncaught error in dropInstance onblocked handler
  • Loading branch information
tofumatt committed Aug 18, 2021
2 parents c1cc34f + 5511168 commit ff5e392
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/drivers/indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,16 @@ function _getConnection(dbInfo, upgradeNeeded) {
};

openreq.onsuccess = function() {
resolve(openreq.result);
var db = openreq.result;
db.onversionchange = function(e) {
// Triggered when the database is modified (e.g. adding an objectStore) or
// deleted (even when initiated by other sessions in different tabs).
// Closing the connection here prevents those operations from being blocked.
// If the database is accessed again later by this instance, the connection
// will be reopened or the database recreated as needed.
e.target.close();
};
resolve(db);
_advanceReadiness(dbInfo);
};
});
Expand Down Expand Up @@ -984,12 +993,22 @@ function dropInstance(options, callback) {
const dropDBPromise = new Promise((resolve, reject) => {
var req = idb.deleteDatabase(options.name);

req.onerror = req.onblocked = err => {
req.onerror = () => {
const db = req.result;
if (db) {
db.close();
}
reject(err);
reject(req.error);
};

req.onblocked = () => {
// Closing all open connections in onversionchange handler should prevent this situation, but if
// we do get here, it just means the request remains pending - eventually it will succeed or error
console.warn(
'dropInstance blocked for database "' +
options.name +
'" until all open connections are closed'
);
};

req.onsuccess = () => {
Expand Down

0 comments on commit ff5e392

Please sign in to comment.