1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

Code review of indexedDB-based cache storage

This commit is contained in:
Raymond Hill 2019-02-26 10:37:25 -05:00
parent bb99d50830
commit 337b1f81b6
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -140,11 +140,12 @@
// Reassign API entries to that of indexedDB-based ones // Reassign API entries to that of indexedDB-based ones
const selectIDB = function() { const selectIDB = function() {
let db;
let dbPromise; let dbPromise;
let dbTimer; let dbTimer;
const genericErrorHandler = function(ev) { const genericErrorHandler = function(ev) {
let error = ev.target && ev.target.error; const error = ev.target && ev.target.error;
if ( error && error.name === 'QuotaExceededError' ) { if ( error && error.name === 'QuotaExceededError' ) {
api.error = error.name; api.error = error.name;
} }
@ -159,13 +160,10 @@
clearTimeout(dbTimer); clearTimeout(dbTimer);
dbTimer = undefined; dbTimer = undefined;
} }
if ( dbPromise === undefined ) { return; } if ( db instanceof IDBDatabase ) {
dbPromise.then(db => { db.close();
if ( db instanceof IDBDatabase ) { db = undefined;
db.close(); }
}
dbPromise = undefined;
});
}; };
const keepAlive = function() { const keepAlive = function() {
@ -196,6 +194,9 @@
const getDb = function() { const getDb = function() {
keepAlive(); keepAlive();
if ( db !== undefined ) {
return Promise.resolve(db);
}
if ( dbPromise !== undefined ) { if ( dbPromise !== undefined ) {
return dbPromise; return dbPromise;
} }
@ -210,6 +211,8 @@
} catch(ex) { } catch(ex) {
} }
if ( req === undefined ) { if ( req === undefined ) {
db = null;
dbPromise = undefined;
return resolve(null); return resolve(null);
} }
req.onupgradeneeded = function(ev) { req.onupgradeneeded = function(ev) {
@ -224,13 +227,16 @@
}; };
req.onsuccess = function(ev) { req.onsuccess = function(ev) {
req = undefined; req = undefined;
const db = ev.target.result; db = ev.target.result;
db.onerror = db.onabort = genericErrorHandler; db.onerror = db.onabort = genericErrorHandler;
dbPromise = undefined;
resolve(db); resolve(db);
}; };
req.onerror = req.onblocked = function() { req.onerror = req.onblocked = function() {
req = undefined; req = undefined;
console.log(this.error); console.log(this.error);
db = null;
dbPromise = undefined;
resolve(null); resolve(null);
}; };
}); });
@ -416,25 +422,15 @@
if ( typeof callback !== 'function' ) { if ( typeof callback !== 'function' ) {
callback = noopfn; callback = noopfn;
} }
getDb().then(db => { disconnect();
if ( !db ) { return callback(); } try {
const finish = ( ) => { const req = indexedDB.deleteDatabase(STORAGE_NAME);
disconnect(); req.onsuccess = req.onerror = ( ) => {
indexedDB.deleteDatabase(STORAGE_NAME); callback();
if ( callback === undefined ) { return; }
let cb = callback;
callback = undefined;
cb();
}; };
try { } catch(ex) {
const req = db.transaction(STORAGE_NAME, 'readwrite') callback();
.objectStore(STORAGE_NAME) }
.clear();
req.onsuccess = req.onerror = finish;
} catch (ex) {
finish();
}
});
}; };
return getDb().then(db => { return getDb().then(db => {