fix logger bug

This commit is contained in:
Julian Freeman
2026-03-14 22:17:55 -04:00
parent 569b13d820
commit 9803ec67ca
6 changed files with 178 additions and 85 deletions

View File

@@ -5,6 +5,7 @@ import { listen } from '@tauri-apps/api/event'
const sortByName = (a: any, b: any) => a.name.localeCompare(b.name, 'zh-CN', { sensitivity: 'accent' });
export interface LogEntry {
id: string; // 日志唯一标识
timestamp: string;
command: string;
output: string;
@@ -41,13 +42,11 @@ export const useSoftwareStore = defineStore('software', {
actionLabel = '已安装';
}
}
return { ...item, status: displayStatus, actionLabel };
});
},
sortedUpdates: (state) => [...state.updates].sort(sortByName),
sortedAllSoftware: (state) => [...state.allSoftware].sort(sortByName),
// 全局繁忙状态:只要有任何软件在等待或安装中,就锁定关键操作
isBusy: (state) => {
const allItems = [...state.essentials, ...state.updates, ...state.allSoftware];
return allItems.some(item => item.status === 'pending' || item.status === 'installing');
@@ -66,7 +65,7 @@ export const useSoftwareStore = defineStore('software', {
}
},
toggleSelection(id: string, type: 'essential' | 'update') {
if (this.isBusy) return; // 繁忙时禁止修改勾选
if (this.isBusy) return;
const list = type === 'essential' ? this.selectedEssentialIds : this.selectedUpdateIds;
const index = list.indexOf(id);
if (index === -1) list.push(id);
@@ -146,12 +145,8 @@ export const useSoftwareStore = defineStore('software', {
}
},
async install(id: string) {
// 这里的 logic 同时负责单个安装和批量安装的任务入队
const software = this.findSoftware(id)
if (software) {
// 进入队列前统一标记为等待中
software.status = 'pending';
}
if (software) software.status = 'pending';
await invoke('install_software', { id })
},
findSoftware(id: string) {
@@ -177,9 +172,24 @@ export const useSoftwareStore = defineStore('software', {
}
})
// 日志监听:根据 ID 追加内容
listen('log-event', (event: any) => {
this.logs.unshift(event.payload as LogEntry);
if (this.logs.length > 200) this.logs.pop();
const payload = event.payload as LogEntry;
const existingLog = this.logs.find(l => l.id === payload.id);
if (existingLog) {
// 如果是增量更新
if (payload.output) {
existingLog.output += '\n' + payload.output;
}
if (payload.status !== 'info') {
existingLog.status = payload.status;
}
} else {
// 如果是新日志
this.logs.unshift(payload);
if (this.logs.length > 100) this.logs.pop();
}
})
}
}