refactor(AppUpdater): remove mainWindow dependency and utilize windowService (#9091)

- Updated AppUpdater to eliminate the mainWindow parameter from the constructor.
- Replaced direct mainWindow references with windowService calls to retrieve the main window, improving modularity and decoupling.
This commit is contained in:
beyondkmp 2025-08-12 23:09:37 +08:00 committed by GitHub
parent 399118174e
commit e4e1325b08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 7 deletions

View File

@ -70,7 +70,7 @@ const memoryService = MemoryService.getInstance()
const dxtService = new DxtService()
export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
const appUpdater = new AppUpdater(mainWindow)
const appUpdater = new AppUpdater()
const notificationService = new NotificationService(mainWindow)
// Initialize Python service with main window

View File

@ -9,6 +9,7 @@ import { CancellationToken, UpdateInfo } from 'builder-util-runtime'
import { app, BrowserWindow, dialog } from 'electron'
import { AppUpdater as _AppUpdater, autoUpdater, Logger, NsisUpdater, UpdateCheckResult } from 'electron-updater'
import path from 'path'
import { windowService } from './WindowService'
import icon from '../../../build/icon.png?asset'
import { configManager } from './ConfigManager'
@ -21,7 +22,7 @@ export default class AppUpdater {
private cancellationToken: CancellationToken = new CancellationToken()
private updateCheckResult: UpdateCheckResult | null = null
constructor(mainWindow: BrowserWindow) {
constructor() {
autoUpdater.logger = logger as Logger
autoUpdater.forceDevUpdateConfig = !app.isPackaged
autoUpdater.autoDownload = configManager.getAutoUpdate()
@ -33,12 +34,12 @@ export default class AppUpdater {
autoUpdater.on('error', (error) => {
logger.error('update error', error as Error)
mainWindow.webContents.send(IpcChannel.UpdateError, error)
windowService.getMainWindow()?.webContents.send(IpcChannel.UpdateError, error)
})
autoUpdater.on('update-available', (releaseInfo: UpdateInfo) => {
logger.info('update available', releaseInfo)
mainWindow.webContents.send(IpcChannel.UpdateAvailable, releaseInfo)
windowService.getMainWindow()?.webContents.send(IpcChannel.UpdateAvailable, releaseInfo)
})
// 检测到不需要更新时
@ -49,17 +50,17 @@ export default class AppUpdater {
return
}
mainWindow.webContents.send(IpcChannel.UpdateNotAvailable)
windowService.getMainWindow()?.webContents.send(IpcChannel.UpdateNotAvailable)
})
// 更新下载进度
autoUpdater.on('download-progress', (progress) => {
mainWindow.webContents.send(IpcChannel.DownloadProgress, progress)
windowService.getMainWindow()?.webContents.send(IpcChannel.DownloadProgress, progress)
})
// 当需要更新的内容下载完成后
autoUpdater.on('update-downloaded', (releaseInfo: UpdateInfo) => {
mainWindow.webContents.send(IpcChannel.UpdateDownloaded, releaseInfo)
windowService.getMainWindow()?.webContents.send(IpcChannel.UpdateDownloaded, releaseInfo)
this.releaseInfo = releaseInfo
logger.info('update downloaded', releaseInfo)
})