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,44 +55,46 @@ export default defineComponent({
const sideViewHook = inject('sideViewHook', null); const sideViewHook = inject('sideViewHook', null);
//#region Listen message from SW //#region Listen message from SW
navigator.serviceWorker.addEventListener('message', ev => { if ('serviceWorker' in navigator) {
if (_DEV_) { navigator.serviceWorker.addEventListener('message', ev => {
console.log('sw msg', ev.data); if (_DEV_) {
} console.log('sw msg', ev.data);
}
const data = ev.data as SwMessage; const data = ev.data as SwMessage;
if (data.type !== 'order') return; if (data.type !== 'order') return;
if (data.loginId !== $i?.id) { if (data.loginId !== $i?.id) {
return getAccountFromId(data.loginId).then(account => { return getAccountFromId(data.loginId).then(account => {
if (!account) return; if (!account) return;
return login(account.token, data.url); return login(account.token, data.url);
}); });
} }
switch (data.order) { switch (data.order) {
case 'post': case 'post':
return post(data.options); return post(data.options);
case 'push': case 'push':
if (data.url.startsWith('/my/messaging')) { if (data.url.startsWith('/my/messaging')) {
if (router.currentRoute.value.path === data.url) return; if (router.currentRoute.value.path === data.url) return;
if (ColdDeviceStorage.get('chatOpenBehavior') === 'window') return pageWindow(data.url); if (ColdDeviceStorage.get('chatOpenBehavior') === 'window') return pageWindow(data.url);
if (ColdDeviceStorage.get('chatOpenBehavior') === 'popout') return popout(data.url); if (ColdDeviceStorage.get('chatOpenBehavior') === 'popout') return popout(data.url);
} }
if (router.currentRoute.value.path === data.url) { if (router.currentRoute.value.path === data.url) {
return window.scroll({ top: 0, behavior: 'smooth' }); return window.scroll({ top: 0, behavior: 'smooth' });
} }
if (navHook) { if (navHook) {
return navHook(data.url); return navHook(data.url);
} }
if (sideViewHook && defaultStore.state.defaultSideView && data.url !== '/') { if (sideViewHook && defaultStore.state.defaultSideView && data.url !== '/') {
return sideViewHook(data.url); return sideViewHook(data.url);
} }
return router.push(data.url); return router.push(data.url);
default: default:
return; return;
} }
}); });
}
} }
return { return {