From 46c622fd86bc388cdd2922c91b97b7bf001f8f23 Mon Sep 17 00:00:00 2001 From: Julian Freeman Date: Tue, 31 Mar 2026 18:49:49 -0400 Subject: [PATCH] optimize fetch time --- src/store/software.ts | 44 +++++++++++++++++++++++++++------------- src/views/Essentials.vue | 4 +++- src/views/Updates.vue | 4 +++- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/store/software.ts b/src/store/software.ts index 548b51f..0135a1a 100644 --- a/src/store/software.ts +++ b/src/store/software.ts @@ -46,7 +46,8 @@ export const useSoftwareStore = defineStore('software', { isInitialized: false, initStatus: '正在检查系统环境...', lastFetched: 0, - refreshTimer: null as any + refreshTimer: null as any, + batchQueue: [] as string[] }), getters: { mergedEssentials: (state) => { @@ -246,23 +247,28 @@ export const useSoftwareStore = defineStore('software', { } }, - // 引入防抖刷新:在批量安装期间,仅在最后一次成功后的 5 秒执行全量扫描 + // 注册批量任务 + startBatch(ids: string[]) { + this.batchQueue = [...ids]; + }, + + // 引入防抖刷新:仅在批量任务全部处理完后的 2 秒执行全量扫描 scheduleDataRefresh() { if (this.refreshTimer) clearTimeout(this.refreshTimer); this.refreshTimer = setTimeout(async () => { - // 执行全量刷新 await this.fetchAllData(); - // 刷新完成后清理所有已成功的任务状态,让卡片恢复正常显示 + // 刷新完成后清理所有已终结(成功或失败)的任务状态快照 Object.keys(this.activeTasks).forEach(id => { - if (this.activeTasks[id].status === 'success') { + const status = this.activeTasks[id].status; + if (status === 'success' || status === 'error') { delete this.activeTasks[id]; } }); this.refreshTimer = null; - }, 5000); + }, 2000); }, findSoftware(id: string) { @@ -279,14 +285,24 @@ export const useSoftwareStore = defineStore('software', { const task = this.activeTasks[id]; this.activeTasks[id] = { status, progress, targetVersion: task?.targetVersion }; - if (status === 'success') { - this.lastFetched = 0; - // 立即更新勾选状态,提升响应感 - this.selectedEssentialIds = this.selectedEssentialIds.filter(i => i !== id); - this.selectedUpdateIds = this.selectedUpdateIds.filter(i => i !== id); - - // 触发防抖刷新 - this.scheduleDataRefresh(); + // 当任务达到终态(成功或失败)时 + if (status === 'success' || status === 'error') { + if (status === 'success') { + this.lastFetched = 0; + // 立即更新勾选状态,提升响应感 + this.selectedEssentialIds = this.selectedEssentialIds.filter(i => i !== id); + this.selectedUpdateIds = this.selectedUpdateIds.filter(i => i !== id); + } + + // 检查是否属于正在进行的批量任务 + const index = this.batchQueue.indexOf(id); + if (index !== -1) { + this.batchQueue.splice(index, 1); + // 如果这是批量任务中的最后一个,则触发延迟刷新 + if (this.batchQueue.length === 0) { + this.scheduleDataRefresh(); + } + } } }) diff --git a/src/views/Essentials.vue b/src/views/Essentials.vue index 11c7078..d448bec 100644 --- a/src/views/Essentials.vue +++ b/src/views/Essentials.vue @@ -78,7 +78,9 @@ const selectableItems = computed(() => { }); const installSelected = () => { - store.selectedEssentialIds.forEach(id => { + const ids = [...store.selectedEssentialIds]; + store.startBatch(ids); + ids.forEach(id => { const item = store.mergedEssentials.find(s => s.id === id); if (item) { store.install(id, item.targetVersion); diff --git a/src/views/Updates.vue b/src/views/Updates.vue index 1c9457e..8c2122a 100644 --- a/src/views/Updates.vue +++ b/src/views/Updates.vue @@ -78,7 +78,9 @@ import { onMounted } from 'vue'; const store = useSoftwareStore(); const updateSelected = () => { - store.selectedUpdateIds.forEach(id => { + const ids = [...store.selectedUpdateIds]; + store.startBatch(ids); + ids.forEach(id => { const item = store.sortedUpdates.find(s => s.id === id); if (item && item.targetVersion) { store.install(id, item.targetVersion);