fix: update cache + timeline when items are updated/deleted (#556)

This commit is contained in:
Daniel Roe 2022-12-25 15:52:37 +01:00 committed by GitHub
parent 720b5114af
commit dc76ab2477
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 15 deletions

View file

@ -36,6 +36,7 @@ const toggleTranslation = async () => {
isLoading.translation = false isLoading.translation = false
} }
const masto = useMasto()
const copyLink = async (status: Status) => { const copyLink = async (status: Status) => {
const url = getStatusPermalinkRoute(status) const url = getStatusPermalinkRoute(status)
if (url) if (url)
@ -50,9 +51,10 @@ const deleteStatus = async () => {
return return
} }
await useMasto().statuses.remove(status.id) removeCachedStatus(status.id)
await masto.statuses.remove(status.id)
if (route.name === '@account-status') if (route.name === 'status')
router.back() router.back()
// TODO when timeline, remove this item // TODO when timeline, remove this item
@ -67,7 +69,8 @@ const deleteAndRedraft = async () => {
return return
} }
const { text } = await useMasto().statuses.remove(status.id) removeCachedStatus(status.id)
const { text } = await masto.statuses.remove(status.id)
openPublishDialog('dialog', await getDraftFromStatus(status, text), true) openPublishDialog('dialog', await getDraftFromStatus(status, text), true)
} }

View file

@ -13,6 +13,9 @@ export function setCached(key: string, value: any, override = false) {
if (override || !cache.has(key)) if (override || !cache.has(key))
cache.set(key, value) cache.set(key, value)
} }
function removeCached(key: string) {
cache.delete(key)
}
export function fetchStatus(id: string, force = false): Promise<Status> { export function fetchStatus(id: string, force = false): Promise<Status> {
const server = currentServer.value const server = currentServer.value
@ -82,6 +85,10 @@ export function cacheStatus(status: Status, server = currentServer.value, overri
setCached(`${server}:status:${status.id}`, status, override) setCached(`${server}:status:${status.id}`, status, override)
} }
export function removeCachedStatus(id: string, server = currentServer.value) {
removeCached(`${server}:status:${id}`)
}
export function cacheAccount(account: Account, server = currentServer.value, override?: boolean) { export function cacheAccount(account: Account, server = currentServer.value, override?: boolean) {
setCached(`${server}:account:${account.id}`, account, override) setCached(`${server}:account:${account.id}`, account, override)
setCached(`${server}:account:${account.acct}`, account, override) setCached(`${server}:account:${account.acct}`, account, override)

View file

@ -79,9 +79,6 @@ export function getAccountRoute(account: Account) {
server: currentServer.value, server: currentServer.value,
account: extractAccountHandle(account), account: extractAccountHandle(account),
}, },
state: {
account: account as any,
},
}) })
} }
export function getAccountFollowingRoute(account: Account) { export function getAccountFollowingRoute(account: Account) {
@ -91,9 +88,6 @@ export function getAccountFollowingRoute(account: Account) {
server: currentServer.value, server: currentServer.value,
account: extractAccountHandle(account), account: extractAccountHandle(account),
}, },
state: {
account: account as any,
},
}) })
} }
export function getAccountFollowersRoute(account: Account) { export function getAccountFollowersRoute(account: Account) {
@ -103,9 +97,6 @@ export function getAccountFollowersRoute(account: Account) {
server: currentServer.value, server: currentServer.value,
account: extractAccountHandle(account), account: extractAccountHandle(account),
}, },
state: {
account: account as any,
},
}) })
} }
@ -117,9 +108,6 @@ export function getStatusRoute(status: Status) {
account: extractAccountHandle(status.account), account: extractAccountHandle(status.account),
status: status.id, status: status.id,
}, },
state: {
status: status as any,
},
}) })
} }

View file

@ -20,16 +20,29 @@ export function usePaginator<T>(paginator: Paginator<any, T[]>, stream?: WsEvent
} }
stream?.on(eventType, (status) => { stream?.on(eventType, (status) => {
if ('uri' in status)
cacheStatus(status, undefined, true)
prevItems.value.unshift(status as any) prevItems.value.unshift(status as any)
}) })
// TODO: update statuses // TODO: update statuses
stream?.on('status.update', (status) => { stream?.on('status.update', (status) => {
cacheStatus(status, undefined, true)
const index = items.value.findIndex((s: any) => s.id === status.id) const index = items.value.findIndex((s: any) => s.id === status.id)
if (index >= 0) if (index >= 0)
items.value[index] = status as any items.value[index] = status as any
}) })
stream?.on('delete', (id) => {
removeCachedStatus(id)
const index = items.value.findIndex((s: any) => s.id === id)
if (index >= 0)
items.value.splice(index, 1)
})
async function loadNext() { async function loadNext() {
if (state.value !== 'idle') if (state.value !== 'idle')
return return

View file

@ -33,11 +33,13 @@ export function useStatusActions(props: StatusActionsProps) {
isLoading[action] = true isLoading[action] = true
fetchNewStatus().then((newStatus) => { fetchNewStatus().then((newStatus) => {
Object.assign(status, newStatus) Object.assign(status, newStatus)
cacheStatus(newStatus, undefined, true)
}).finally(() => { }).finally(() => {
isLoading[action] = false isLoading[action] = false
}) })
// Optimistic update // Optimistic update
status[action] = !status[action] status[action] = !status[action]
cacheStatus(status, undefined, true)
if (countField) if (countField)
status[countField] += status[action] ? 1 : -1 status[countField] += status[action] ? 1 : -1
} }