fix(electron): 修复客户端下载功能无法启动的问题

- 将 onRenderer 参数从 mainWindow 改为 getMainWindow 函数,解决模块加载时 mainWindow 为 null 导致下载无法触发的问题
  - 处理 InterruptedError 错误,避免下载中断时抛出未处理异常
This commit is contained in:
kuaifan 2026-01-15 23:50:36 +08:00
parent bc460f0da8
commit 2163bb0bff
3 changed files with 8 additions and 6 deletions

View File

@ -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;

View File

@ -1060,4 +1060,4 @@ ipcMain.on('updateQuitAndInstall', (event, args) => {
//================================================================
onExport()
onRenderer(mainWindow)
onRenderer(() => mainWindow)

View File

@ -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);