Fix: db initializing error, polyfill localStorage/sessionStorage (#616)
* Fix: make sure all errors are caught when initializing * Fix: polyfill localStorage and sessionStorage
This commit is contained in:
parent
514fa3204f
commit
c61806974f
|
@ -18,7 +18,7 @@ const db = (() => {
|
||||||
};
|
};
|
||||||
|
|
||||||
function prepare() {
|
function prepare() {
|
||||||
return shouldUseIndexedDB().then(
|
return withPromise(shouldUseIndexedDB).then(
|
||||||
ok => {
|
ok => {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
useIndexedDB();
|
useIndexedDB();
|
||||||
|
@ -37,16 +37,18 @@ const db = (() => {
|
||||||
// which, once detected on the first run, is remembered in chrome.storage.local
|
// which, once detected on the first run, is remembered in chrome.storage.local
|
||||||
// for reliablility and in localStorage for fast synchronous access
|
// for reliablility and in localStorage for fast synchronous access
|
||||||
// (FF may block localStorage depending on its privacy options)
|
// (FF may block localStorage depending on its privacy options)
|
||||||
|
// note that it may throw when accessing the variable
|
||||||
|
// https://github.com/openstyles/stylus/issues/615
|
||||||
if (typeof indexedDB === 'undefined') {
|
if (typeof indexedDB === 'undefined') {
|
||||||
return Promise.reject(new Error('indexedDB is undefined'));
|
throw new Error('indexedDB is undefined');
|
||||||
}
|
}
|
||||||
// test localStorage
|
// test localStorage
|
||||||
const fallbackSet = localStorage.dbInChromeStorage;
|
const fallbackSet = localStorage.dbInChromeStorage;
|
||||||
if (fallbackSet === 'true') {
|
if (fallbackSet === 'true') {
|
||||||
return Promise.resolve(false);
|
return false;
|
||||||
}
|
}
|
||||||
if (fallbackSet === 'false') {
|
if (fallbackSet === 'false') {
|
||||||
return Promise.resolve(true);
|
return true;
|
||||||
}
|
}
|
||||||
// test storage.local
|
// test storage.local
|
||||||
return chromeLocal.get('dbInChromeStorage')
|
return chromeLocal.get('dbInChromeStorage')
|
||||||
|
@ -59,6 +61,14 @@ const db = (() => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function withPromise(fn) {
|
||||||
|
try {
|
||||||
|
return Promise.resolve(fn());
|
||||||
|
} catch (err) {
|
||||||
|
return Promise.reject(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function testDBSize() {
|
function testDBSize() {
|
||||||
return dbExecIndexedDB('getAllKeys', IDBKeyRange.lowerBound(1), 1)
|
return dbExecIndexedDB('getAllKeys', IDBKeyRange.lowerBound(1), 1)
|
||||||
.then(event => (
|
.then(event => (
|
||||||
|
|
|
@ -50,4 +50,22 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (!localStorage) {
|
||||||
|
throw new Error('localStorage is null');
|
||||||
|
}
|
||||||
|
localStorage._access_check = 1;
|
||||||
|
delete localStorage._access_check;
|
||||||
|
} catch (err) {
|
||||||
|
Object.defineProperty(self, 'localStorage', {value: {}});
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!sessionStorage) {
|
||||||
|
throw new Error('sessionStorage is null');
|
||||||
|
}
|
||||||
|
sessionStorage._access_check = 1;
|
||||||
|
delete sessionStorage._access_check;
|
||||||
|
} catch (err) {
|
||||||
|
Object.defineProperty(self, 'sessionStorage', {value: {}});
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user