refactor(auto-update): should triggle download when checking for update manually (#5262)

refactor(auto-update): streamline update check logic and enhance error handling

- Moved update check logic from IPC handler to AppUpdater class for better organization.
- Improved error handling during update checks to ensure graceful failure.
- Removed redundant conditions in the AboutSettings component for cleaner rendering.
This commit is contained in:
beyondkmp 2025-04-25 21:21:44 +08:00 committed by GitHub
parent a7ee3cbd02
commit a112b143e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 14 deletions

View File

@ -178,19 +178,7 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
// check for update
ipcMain.handle(IpcChannel.App_CheckForUpdate, async () => {
if (isWin && 'PORTABLE_EXECUTABLE_DIR' in process.env) {
return {
currentVersion: app.getVersion(),
updateInfo: null
}
}
const update = await appUpdater.autoUpdater.checkForUpdates()
return {
currentVersion: appUpdater.autoUpdater.currentVersion,
updateInfo: update?.updateInfo
}
await appUpdater.checkForUpdates()
})
// zip

View File

@ -1,3 +1,4 @@
import { isWin } from '@main/constant'
import { IpcChannel } from '@shared/IpcChannel'
import { UpdateInfo } from 'builder-util-runtime'
import { app, BrowserWindow, dialog } from 'electron'
@ -60,6 +61,35 @@ export default class AppUpdater {
autoUpdater.autoInstallOnAppQuit = isActive
}
public async checkForUpdates() {
if (isWin && 'PORTABLE_EXECUTABLE_DIR' in process.env) {
return {
currentVersion: app.getVersion(),
updateInfo: null
}
}
try {
const update = await this.autoUpdater.checkForUpdates()
if (update?.updateInfo && !this.autoUpdater.autoDownload) {
// 如果 autoDownload 为 false则需要再调用下面的函数触发下
// do not use await, because it will block the return of this function
this.autoUpdater.downloadUpdate()
}
return {
currentVersion: this.autoUpdater.currentVersion,
updateInfo: update?.updateInfo
}
} catch (error) {
logger.error('Failed to check for update:', error)
return {
currentVersion: app.getVersion(),
updateInfo: null
}
}
}
public async showUpdateDialog(mainWindow: BrowserWindow) {
if (!this.releaseInfo) {
return

View File

@ -160,7 +160,7 @@ const AboutSettings: FC = () => {
</>
)}
</SettingGroup>
{autoCheckUpdate && hasNewVersion && update.info && (
{hasNewVersion && update.info && (
<SettingGroup theme={theme}>
<SettingRow>
<SettingRowTitle>