From a112b143e7118b231788c653b63c474d7906341e Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 25 Apr 2025 21:21:44 +0800 Subject: [PATCH] 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. --- src/main/ipc.ts | 14 +-------- src/main/services/AppUpdater.ts | 30 +++++++++++++++++++ .../src/pages/settings/AboutSettings.tsx | 2 +- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 41a2a3bee9..47e0b25386 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -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 diff --git a/src/main/services/AppUpdater.ts b/src/main/services/AppUpdater.ts index 35418bf587..8e2c1deddd 100644 --- a/src/main/services/AppUpdater.ts +++ b/src/main/services/AppUpdater.ts @@ -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 diff --git a/src/renderer/src/pages/settings/AboutSettings.tsx b/src/renderer/src/pages/settings/AboutSettings.tsx index 42bb5690f3..62a4971caa 100644 --- a/src/renderer/src/pages/settings/AboutSettings.tsx +++ b/src/renderer/src/pages/settings/AboutSettings.tsx @@ -160,7 +160,7 @@ const AboutSettings: FC = () => { )} - {autoCheckUpdate && hasNewVersion && update.info && ( + {hasNewVersion && update.info && (