fix indexeddb available detection

This commit is contained in:
tamaina 2021-07-27 01:44:50 +09:00
parent 66b4eaf72e
commit 8e14463215
5 changed files with 27 additions and 15 deletions

View file

@ -48,10 +48,10 @@ export default defineComponent({
title: this.$ts.accounts, title: this.$ts.accounts,
icon: 'fas fa-users', icon: 'fas fa-users',
}, },
storedAccounts: getAccounts().filter(x => x.id !== this.$i.id), storedAccounts: getAccounts().then(accounts => accounts.filter(x => x.id !== this.$i.id)),
accounts: null, accounts: null,
init: () => os.api('users/show', { init: async () => os.api('users/show', {
userIds: this.storedAccounts.map(x => x.id) userIds: (await this.storedAccounts).map(x => x.id)
}).then(accounts => { }).then(accounts => {
this.accounts = accounts; this.accounts = accounts;
}), }),
@ -104,8 +104,8 @@ export default defineComponent({
}, 'closed'); }, 'closed');
}, },
switchAccount(account: any) { async switchAccount(account: any) {
const storedAccounts = getAccounts(); const storedAccounts = await getAccounts();
const token = storedAccounts.find(x => x.id === account.id).token; const token = storedAccounts.find(x => x.id === account.id).token;
this.switchAccountWithToken(token); this.switchAccountWithToken(token);
}, },

View file

@ -4,21 +4,33 @@ import {
get as iget, get as iget,
set as iset, set as iset,
del as idel, del as idel,
createStore
} from 'idb-keyval'; } from 'idb-keyval';
const fallbackName = (key: string) => `idbfallback::${key}`; const fallbackName = (key: string) => `idbfallback::${key}`;
let idbAvailable = typeof window !== 'undefined' ? !!window.indexedDB : true; let idbAvailable = typeof window !== 'undefined' ? !!window.indexedDB : true;
console.log(window.indexedDB);
console.log(idbAvailable);
if (idbAvailable) { if (idbAvailable) {
try { try {
const request = indexedDB.open('keyval-store'); const request = indexedDB.open('keyval-store');
if (request.error) idbAvailable = false;
await new Promise((res, rej) => {
request.onerror = (e) => rej(e);
request.onsuccess = (e) => res(e);
});
} catch (e) { } catch (e) {
console.log('catch', e)
idbAvailable = false; idbAvailable = false;
} }
} }
console.log(idbAvailable);
if (!idbAvailable) console.error('indexedDB is unavailable. It will use localStorage.');
export async function get(key: string) { export async function get(key: string) {
if (idbAvailable) return iget(key); if (idbAvailable) return iget(key);
return JSON.parse(localStorage.getItem(fallbackName(key))); return JSON.parse(localStorage.getItem(fallbackName(key)));

View file

@ -135,7 +135,7 @@ export default defineComponent({
}, },
async openAccountMenu(ev) { async openAccountMenu(ev) {
const storedAccounts = getAccounts().filter(x => x.id !== this.$i.id); const storedAccounts = await getAccounts().then(accounts => accounts.filter(x => x.id !== this.$i.id));
const accountsPromise = os.api('users/show', { userIds: storedAccounts.map(x => x.id) }); const accountsPromise = os.api('users/show', { userIds: storedAccounts.map(x => x.id) });
const accountItemPromises = storedAccounts.map(a => new Promise(res => { const accountItemPromises = storedAccounts.map(a => new Promise(res => {
@ -195,8 +195,8 @@ export default defineComponent({
}, 'closed'); }, 'closed');
}, },
switchAccount(account: any) { async switchAccount(account: any) {
const storedAccounts = getAccounts(); const storedAccounts = await getAccounts();
const token = storedAccounts.find(x => x.id === account.id).token; const token = storedAccounts.find(x => x.id === account.id).token;
this.switchAccountWithToken(token); this.switchAccountWithToken(token);
}, },

View file

@ -101,7 +101,7 @@ export default defineComponent({
}, },
async openAccountMenu(ev) { async openAccountMenu(ev) {
const storedAccounts = getAccounts().filter(x => x.id !== this.$i.id); const storedAccounts = await getAccounts().then(accounts => accounts.filter(x => x.id !== this.$i.id));
const accountsPromise = os.api('users/show', { userIds: storedAccounts.map(x => x.id) }); const accountsPromise = os.api('users/show', { userIds: storedAccounts.map(x => x.id) });
const accountItemPromises = storedAccounts.map(a => new Promise(res => { const accountItemPromises = storedAccounts.map(a => new Promise(res => {
@ -161,8 +161,8 @@ export default defineComponent({
}, 'closed'); }, 'closed');
}, },
switchAccount(account: any) { async switchAccount(account: any) {
const storedAccounts = getAccounts(); const storedAccounts = await getAccounts();
const token = storedAccounts.find(x => x.id === account.id).token; const token = storedAccounts.find(x => x.id === account.id).token;
this.switchAccountWithToken(token); this.switchAccountWithToken(token);
}, },

View file

@ -121,7 +121,7 @@ export default defineComponent({
}, },
async openAccountMenu(ev) { async openAccountMenu(ev) {
const storedAccounts = getAccounts().filter(x => x.id !== this.$i.id); const storedAccounts = await getAccounts().then(accounts => accounts.filter(x => x.id !== this.$i.id));
const accountsPromise = os.api('users/show', { userIds: storedAccounts.map(x => x.id) }); const accountsPromise = os.api('users/show', { userIds: storedAccounts.map(x => x.id) });
const accountItemPromises = storedAccounts.map(a => new Promise(res => { const accountItemPromises = storedAccounts.map(a => new Promise(res => {
@ -181,8 +181,8 @@ export default defineComponent({
}, 'closed'); }, 'closed');
}, },
switchAccount(account: any) { async switchAccount(account: any) {
const storedAccounts = getAccounts(); const storedAccounts = await getAccounts();
const token = storedAccounts.find(x => x.id === account.id).token; const token = storedAccounts.find(x => x.id === account.id).token;
this.switchAccountWithToken(token); this.switchAccountWithToken(token);
}, },