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

View file

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