From 2163bb0bff7efeb90b3916612025eca9a8f658b2 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 15 Jan 2026 23:50:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(electron):=20=E4=BF=AE=E5=A4=8D=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=90=AF=E5=8A=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 onRenderer 参数从 mainWindow 改为 getMainWindow 函数,解决模块加载时 mainWindow 为 null 导致下载无法触发的问题 - 处理 InterruptedError 错误,避免下载中断时抛出未处理异常 --- electron/electron-down.js | 8 +++++--- electron/electron.js | 2 +- electron/lib/renderer.js | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/electron/electron-down.js b/electron/electron-down.js index c0621ffbb..b01059353 100644 --- a/electron/electron-down.js +++ b/electron/electron-down.js @@ -2,7 +2,7 @@ const {BrowserWindow, screen, shell, ipcMain} = require('electron') const fs = require('fs'); const path = require('path'); const loger = require("electron-log"); -const {default: electronDl, download, CancelError} = require("@dootask/electron-dl"); +const {default: electronDl, download, CancelError, InterruptedError} = require("@dootask/electron-dl"); const utils = require("./lib/utils"); const {DownloadManager, DownloadStore} = require("./lib/download-manager"); @@ -116,10 +116,12 @@ async function createDownload(window_, url, options = {}) { try { return await download(window_, url, options); } catch (error) { - // electron-dl rejects with CancelError when a download is cancelled; treat it as expected. + // electron-dl rejects with CancelError/InterruptedError; treat them as expected. const isCancelError = (typeof CancelError === 'function' && error instanceof CancelError) || error?.name === 'CancelError'; - if (!isCancelError) { + const isInterruptedError = (typeof InterruptedError === 'function' && error instanceof InterruptedError) + || error?.name === 'InterruptedError'; + if (!isCancelError && !isInterruptedError) { throw error; } return null; diff --git a/electron/electron.js b/electron/electron.js index e9e60658b..9af28de75 100644 --- a/electron/electron.js +++ b/electron/electron.js @@ -1060,4 +1060,4 @@ ipcMain.on('updateQuitAndInstall', (event, args) => { //================================================================ onExport() -onRenderer(mainWindow) +onRenderer(() => mainWindow) diff --git a/electron/lib/renderer.js b/electron/lib/renderer.js index 5e62e1286..af3a21ab4 100644 --- a/electron/lib/renderer.js +++ b/electron/lib/renderer.js @@ -572,7 +572,7 @@ const renderer = { }, } -const onRenderer = (mainWindow) => { +const onRenderer = (getMainWindow) => { ipcMain.on("rendererReq", async (event, args) => { try { let ret = null; @@ -651,7 +651,7 @@ const onRenderer = (mainWindow) => { ret = await electronDown.updateWindow(args.language, args.theme); break; case 'createDownload': - ret = await electronDown.createDownload(mainWindow, args.url, args.options || {}); + ret = await electronDown.createDownload(getMainWindow(), args.url, args.options || {}); break; case 'watchFile': ret = await renderer.watchFile(args.path);