fix
This commit is contained in:
@@ -2,22 +2,33 @@
|
||||
import {
|
||||
get as iget,
|
||||
set as iset,
|
||||
del as idel
|
||||
del as idel,
|
||||
} from 'idb-keyval';
|
||||
|
||||
const fallbackName = (key: string) => `idbfallback::${key}`;
|
||||
|
||||
let idbAvailable = !!window.indexedDB;
|
||||
|
||||
if (idbAvailable) {
|
||||
try {
|
||||
const request = indexedDB.open('keyval-store');
|
||||
if (request.error) idbAvailable = false;
|
||||
} catch (e) {
|
||||
idbAvailable = false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function get(key: string) {
|
||||
if (window.indexedDB) return iget(key);
|
||||
if (idbAvailable) return iget(key);
|
||||
return JSON.parse(localStorage.getItem(fallbackName(key)));
|
||||
}
|
||||
|
||||
export async function set(key: string, val: any) {
|
||||
if (window.indexedDB) return iset(key, val);
|
||||
if (idbAvailable) return iset(key, val);
|
||||
return localStorage.setItem(fallbackName(key), JSON.stringify(val));
|
||||
}
|
||||
|
||||
export async function del(key: string) {
|
||||
if (window.indexedDB) return idel(key);
|
||||
if (idbAvailable) return idel(key);
|
||||
return localStorage.removeItem(fallbackName(key));
|
||||
}
|
||||
|
Reference in New Issue
Block a user