improve avatar time spent loading performance by fetching all times in one query

This commit is contained in:
pa
2026-03-23 11:40:41 +09:00
parent 369f5130b5
commit 1895d0f25c
2 changed files with 23 additions and 11 deletions

View File

@@ -53,6 +53,18 @@ const avatarFavorites = {
return ref;
},
async getAllAvatarTimeSpent() {
const map = new Map();
await sqliteService.execute(
(row) => {
map.set(row[0], row[1] || 0);
},
`SELECT avatar_id, time FROM ${dbVars.userPrefix}_avatar_history`
);
return map;
},
addAvatarTimeSpent(avatarId, timeSpent) {
sqliteService.executeNonQuery(
`UPDATE ${dbVars.userPrefix}_avatar_history SET time = time + @timeSpent WHERE avatar_id = @avatarId`,

View File

@@ -839,18 +839,18 @@
const list = Array.from(map.values());
const currentAvatarId = currentUser.value.currentAvatar;
const swapTime = currentUser.value.$previousAvatarSwapTime;
const tagsMap = await database.getAllAvatarTags();
const [tagsMap, avatarTimeSpentMap] = await Promise.all([
database.getAllAvatarTags(),
database.getAllAvatarTimeSpent()
]);
avatarTagsMap.value = tagsMap;
await Promise.all(
list.map(async (ref) => {
const aviTime = await database.getAvatarTimeSpent(ref.id);
ref.$timeSpent = aviTime.timeSpent;
if (ref.id === currentAvatarId && swapTime) {
ref.$timeSpent += Date.now() - swapTime;
}
ref.$tags = tagsMap.get(ref.id) || [];
})
);
for (const ref of list) {
ref.$timeSpent = avatarTimeSpentMap.get(ref.id) || 0;
if (ref.id === currentAvatarId && swapTime) {
ref.$timeSpent += Date.now() - swapTime;
}
ref.$tags = tagsMap.get(ref.id) || [];
}
avatars.value = list;
isLoading.value = false;
}