2018-12-06 05:34:30 +00:00
|
|
|
// LocalStorage and IDB may be disabled in private mode, when "blocking cookies" in Safari,
|
|
|
|
// or other cases
|
|
|
|
|
|
|
|
import { thunk } from './thunk'
|
|
|
|
|
|
|
|
const testKey = '__test__'
|
|
|
|
|
2018-12-08 19:21:54 +00:00
|
|
|
export const testHasLocalStorageOnce = () => {
|
2018-12-06 05:34:30 +00:00
|
|
|
try {
|
|
|
|
localStorage.setItem(testKey, testKey)
|
|
|
|
if (!localStorage.length || localStorage.getItem(testKey) !== testKey) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
localStorage.removeItem(testKey)
|
|
|
|
} catch (e) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-12-08 19:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const testHasLocalStorage = thunk(testHasLocalStorageOnce)
|
2018-12-06 05:34:30 +00:00
|
|
|
|
|
|
|
export const testHasIndexedDB = thunk(async () => {
|
|
|
|
if (typeof indexedDB === 'undefined') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let idbFailed = await new Promise(resolve => {
|
|
|
|
let db = indexedDB.open(testKey)
|
|
|
|
db.onerror = () => resolve(true)
|
|
|
|
db.onsuccess = () => {
|
|
|
|
indexedDB.deleteDatabase(testKey)
|
|
|
|
resolve(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (idbFailed) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|