feat: cache for publish widget

This commit is contained in:
Anthony Fu 2022-11-21 14:55:31 +08:00
parent 5d5cdebb56
commit 193d1cf5c5
9 changed files with 149 additions and 43 deletions

View file

@ -1,5 +1,7 @@
import { login as loginMasto } from 'masto'
import type { UserLogin } from '~/types'
import type { ServerInfo, UserLogin } from '~/types'
const ServerInfoTTL = 60 * 60 * 1000 * 12 // 12 hour
function createClientState() {
const { server, token } = useAppCookies()
@ -43,10 +45,39 @@ function createClientState() {
return true
}
const serverInfos = useLocalStorage<Record<string, ServerInfo>>('nuxtodon-server-info', {})
async function fetchServerInfo(server: string) {
if (!serverInfos.value[server]) {
// @ts-expect-error init
serverInfos.value[server] = {
timeUpdated: 0,
server,
}
}
if (serverInfos.value[server].timeUpdated + ServerInfoTTL < Date.now()) {
const masto = await useMasto()
await Promise.allSettled([
masto.instances.fetch().then((r) => {
Object.assign(serverInfos.value[server], r)
}),
masto.customEmojis.fetchAll().then((r) => {
serverInfos.value[server].customEmojis = Object.fromEntries(r.map(i => [i.shortcode, i]))
}),
])
}
return serverInfos.value[server]
}
if (server.value)
fetchServerInfo(server.value)
return {
currentUser,
accounts,
login,
serverInfos,
fetchServerInfo,
}
}