feat: cache for publish widget
This commit is contained in:
parent
5d5cdebb56
commit
193d1cf5c5
9 changed files with 149 additions and 43 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue