fix sync ui

This commit is contained in:
Julian Freeman
2026-03-14 17:34:43 -04:00
parent 7743a25f7b
commit 65ce05b36b
3 changed files with 107 additions and 29 deletions

View File

@@ -7,7 +7,8 @@ export const useSoftwareStore = defineStore('software', {
essentials: [] as any[],
updates: [] as any[],
allSoftware: [] as any[],
loading: false
loading: false,
lastFetched: 0 // 记录上次完整同步的时间戳
}),
getters: {
mergedEssentials: (state) => {
@@ -49,18 +50,34 @@ export const useSoftwareStore = defineStore('software', {
this.allSoftware = await invoke('get_all_software')
this.loading = false
},
// 智能同步:如果数据较新,则直接返回
async syncDataIfNeeded(force = false) {
const now = Date.now();
const CACHE_TIMEOUT = 5 * 60 * 1000; // 5 分钟缓存
if (!force && this.allSoftware.length > 0 && (now - this.lastFetched < CACHE_TIMEOUT)) {
// 如果不是强制刷新,且数据在 5 分钟内,且已经有数据,则跳过
if (this.essentials.length === 0) await this.fetchEssentials();
return;
}
await this.fetchAllData();
},
async fetchAllData() {
this.loading = true;
// 并行获取三类数据
const [essentials, all, updates] = await Promise.all([
invoke('get_essentials'),
invoke('get_all_software'),
invoke('get_updates')
]);
this.essentials = essentials as any[];
this.allSoftware = all as any[];
this.updates = updates as any[];
this.loading = false;
try {
const [essentials, all, updates] = await Promise.all([
invoke('get_essentials'),
invoke('get_all_software'),
invoke('get_updates')
]);
this.essentials = essentials as any[];
this.allSoftware = all as any[];
this.updates = updates as any[];
this.lastFetched = Date.now();
} finally {
this.loading = false;
}
},
async install(id: string) {
const software = this.findSoftware(id)
@@ -68,11 +85,16 @@ export const useSoftwareStore = defineStore('software', {
await invoke('install_software', { id })
},
findSoftware(id: string) {
// 这里的逻辑会从多个列表中查找对象
return this.essentials.find(s => s.id === id) ||
this.updates.find(s => s.id === id) ||
this.allSoftware.find(s => s.id === id)
},
initListener() {
// 避免重复监听
if ((window as any).__tauri_listener_init) return;
(window as any).__tauri_listener_init = true;
listen('install-status', (event: any) => {
const { id, status, progress } = event.payload
const software = this.findSoftware(id)
@@ -80,6 +102,11 @@ export const useSoftwareStore = defineStore('software', {
software.status = status
software.progress = progress
}
// 如果安装成功,标记数据过期,以便下次切换或刷新时同步最新状态
if (status === 'success') {
this.lastFetched = 0;
}
})
}
}