1
0

feat(status): optimistic update count

This commit is contained in:
三咲智子 2022-11-25 17:23:34 +08:00
parent d3e4e22ed1
commit 5f254b378f
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E

View File

@ -24,9 +24,15 @@ const isLoading = $ref({
pinned: false, pinned: false,
translation: false, translation: false,
}) })
async function toggleStatusAction(action: 'reblogged' | 'favourited' | 'bookmarked' | 'pinned', newStatus: Promise<Status>) {
type Action = 'reblogged' | 'favourited' | 'bookmarked' | 'pinned'
type CountField = 'reblogsCount' | 'favouritesCount'
async function toggleStatusAction(action: Action, newStatus: Promise<Status>, countField?: CountField) {
// Optimistic update // Optimistic update
status[action] = !status[action] status[action] = !status[action]
if (countField)
status[countField] += status[action] ? 1 : -1
try { try {
isLoading[action] = true isLoading[action] = true
Object.assign(status, await newStatus) Object.assign(status, await newStatus)
@ -35,7 +41,6 @@ async function toggleStatusAction(action: 'reblogged' | 'favourited' | 'bookmark
isLoading[action] = false isLoading[action] = false
} }
} }
const toggleReblog = () => toggleStatusAction( const toggleReblog = () => toggleStatusAction(
'reblogged', 'reblogged',
masto.statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => { masto.statuses[status.reblogged ? 'unreblog' : 'reblog'](status.id).then((res) => {
@ -44,11 +49,13 @@ const toggleReblog = () => toggleStatusAction(
return res.reblog! return res.reblog!
return res return res
}), }),
'reblogsCount',
) )
const toggleFavourite = () => toggleStatusAction( const toggleFavourite = () => toggleStatusAction(
'favourited', 'favourited',
masto.statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id), masto.statuses[status.favourited ? 'unfavourite' : 'favourite'](status.id),
'favouritesCount',
) )
const toggleBookmark = () => toggleStatusAction( const toggleBookmark = () => toggleStatusAction(