deal properly with indexedDB not being available (#2925)

This commit is contained in:
gorhill 2017-08-30 08:41:22 -04:00
parent b1842ddf16
commit d165432ded
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 17 additions and 12 deletions

View File

@ -89,16 +89,26 @@ vAPI.cacheStorage = (function() {
function noopfn() { function noopfn() {
} }
function processPendings() {
var cb;
while ( (cb = pending.shift()) ) {
cb(db);
}
}
function getDb(callback) { function getDb(callback) {
if ( pending === undefined ) {
return callback();
}
if ( pending.length !== 0 ) { if ( pending.length !== 0 ) {
pending.push(callback); return pending.push(callback);
return;
} }
if ( db instanceof IDBDatabase ) { if ( db instanceof IDBDatabase ) {
return callback(db); return callback(db);
} }
pending.push(callback); pending.push(callback);
if ( pending.length !== 1 ) { return; } if ( pending.length !== 1 ) { return; }
// This will fail in private browsing mode.
var req = indexedDB.open(STORAGE_NAME, 1); var req = indexedDB.open(STORAGE_NAME, 1);
req.onupgradeneeded = function(ev) { req.onupgradeneeded = function(ev) {
db = ev.target.result; db = ev.target.result;
@ -109,17 +119,12 @@ vAPI.cacheStorage = (function() {
req.onsuccess = function(ev) { req.onsuccess = function(ev) {
db = ev.target.result; db = ev.target.result;
db.onerror = genericErrorHandler; db.onerror = genericErrorHandler;
var cb; processPendings();
while ( (cb = pending.shift()) ) {
cb(db);
}
}; };
req.onerror = function(ev) { req.onerror = function() {
console.log(ev); console.log(this.error);
var cb; processPendings();
while ( (cb = pending.shift()) ) { pending = undefined;
cb(db);
}
}; };
} }