From d165432dedc74f10bd0378082b859be79d54613e Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 30 Aug 2017 08:41:22 -0400 Subject: [PATCH] deal properly with indexedDB not being available (#2925) --- platform/webext/vapi-cachestorage.js | 29 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/platform/webext/vapi-cachestorage.js b/platform/webext/vapi-cachestorage.js index ada79a49d..9420832f5 100644 --- a/platform/webext/vapi-cachestorage.js +++ b/platform/webext/vapi-cachestorage.js @@ -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; }; }