This commit is contained in:
tamaina 2021-02-24 19:27:00 +09:00
parent a7180b3262
commit 40d12949b1
2 changed files with 52 additions and 39 deletions

View file

@ -2,22 +2,33 @@
import { import {
get as iget, get as iget,
set as iset, set as iset,
del as idel del as idel,
} from 'idb-keyval'; } from 'idb-keyval';
const fallbackName = (key: string) => `idbfallback::${key}`; 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) { export async function get(key: string) {
if (window.indexedDB) return iget(key); if (idbAvailable) return iget(key);
return JSON.parse(localStorage.getItem(fallbackName(key))); return JSON.parse(localStorage.getItem(fallbackName(key)));
} }
export async function set(key: string, val: any) { 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)); return localStorage.setItem(fallbackName(key), JSON.stringify(val));
} }
export async function del(key: string) { export async function del(key: string) {
if (window.indexedDB) return idel(key); if (idbAvailable) return idel(key);
return localStorage.removeItem(fallbackName(key)); return localStorage.removeItem(fallbackName(key));
} }

View file

@ -55,6 +55,7 @@ export default defineComponent({
const sideViewHook = inject('sideViewHook', null); const sideViewHook = inject('sideViewHook', null);
//#region Listen message from SW //#region Listen message from SW
if ('serviceWorker' in navigator) {
navigator.serviceWorker.addEventListener('message', ev => { navigator.serviceWorker.addEventListener('message', ev => {
if (_DEV_) { if (_DEV_) {
console.log('sw msg', ev.data); console.log('sw msg', ev.data);
@ -94,6 +95,7 @@ export default defineComponent({
} }
}); });
} }
}
return { return {
uploads, uploads,