Skip to content

Commit

Permalink
fix(cache): return cached account as promise (#2623)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Feb 25, 2024
1 parent c00d6f7 commit 748dd5e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions composables/cache.ts
Expand Up @@ -23,7 +23,8 @@ export function fetchStatus(id: string, force = false): Promise<mastodon.v1.Stat
const key = `${server}:${userId}:status:${id}`
const cached = cache.get(key)
if (cached && !force)
return cached
return Promise.resolve(cached)

const promise = useMastoClient().v1.statuses.$select(id).fetch()
.then((status) => {
cacheStatus(status)
Expand All @@ -42,7 +43,8 @@ export function fetchAccountById(id?: string | null): Promise<mastodon.v1.Accoun
const key = `${server}:${userId}:account:${id}`
const cached = cache.get(key)
if (cached)
return cached
return Promise.resolve(cached)

const domain = getInstanceDomainFromServer(server)
const promise = useMastoClient().v1.accounts.$select(id).fetch()
.then((r) => {
Expand All @@ -64,7 +66,7 @@ export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Ac
const key = `${server}:${userId}:account:${userAcct}`
const cached = cache.get(key)
if (cached)
return cached
return Promise.resolve(cached)

async function lookupAccount() {
const client = useMastoClient()
Expand All @@ -82,13 +84,13 @@ export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Ac
return account
}

const account = lookupAccount()
const promise = lookupAccount()
.then((r) => {
cacheAccount(r, server, true)
return r
})
cache.set(key, account)
return account
cache.set(key, promise)
return promise
}

export function useAccountById(id?: string | null) {
Expand Down

0 comments on commit 748dd5e

Please sign in to comment.