perf: 优化客户端升级

This commit is contained in:
kuaifan 2024-11-14 00:33:44 +08:00
parent 99027858d9
commit 2ca35e4458
3 changed files with 68 additions and 1 deletions

1
electron/.gitignore vendored
View File

@ -5,6 +5,7 @@ package-lock.json
build/
dist/
updater/*
.devload
.native

67
electron/electron.js vendored
View File

@ -20,6 +20,7 @@ const isMac = process.platform === 'darwin'
const isWin = process.platform === 'win32'
const allowedUrls = /^(?:https?|mailto|tel|callto):/i;
const allowedCalls = /^(?:mailto|tel|callto):/i;
let updaterLockFile = path.join(os.tmpdir(), '.dootask_updater.lock');
let enableStoreBkp = true;
let dialogOpen = false;
let enablePlugins = false;
@ -183,6 +184,55 @@ function createMainWindow() {
})
}
/**
* 创建更新程序子进程
*/
function createUpdaterWindow() {
// 检查平台是否支持
if (!['darwin', 'win32'].includes(process.platform)) {
return;
}
try {
// 构建updater应用路径
let updaterPath;
if (isWin) {
updaterPath = path.join(__dirname, 'updater', 'updater.exe');
} else {
updaterPath = path.join(__dirname, 'updater', 'updater');
}
// 检查updater应用是否存在
if (!fs.existsSync(updaterPath)) {
console.log('Updater not found:', updaterPath);
return;
}
// 创建锁文件
fs.writeFileSync(updaterLockFile, '1');
// 启动子进程,传入锁文件路径作为第一个参数
const child = spawn(updaterPath, [updaterLockFile], {
detached: true,
stdio: 'ignore',
env: {
...process.env,
ELECTRON_RUN_AS_NODE: '1',
UPDATER_LOCK_FILE: updaterLockFile
}
});
child.unref();
child.on('error', (err) => {
console.log('Updater process error:', err);
});
} catch (e) {
console.log('Failed to create updater process:', e);
}
}
/**
* 创建子窗口
* @param args {path, hash, title, titleFixed, force, userAgent, config, webPreferences}
@ -686,6 +736,14 @@ if (!getTheLock) {
mainTray.setContextMenu(trayMenu)
}
}
// 删除updater锁文件(如果存在)
if (fs.existsSync(updaterLockFile)) {
try {
fs.unlinkSync(updaterLockFile);
} catch (e) {
//忽略错误
}
}
//
if (process.platform === 'win32') {
app.setAppUserModelId(config.name)
@ -1215,13 +1273,20 @@ ipcMain.on('mainWindowActive', (event) => {
*/
ipcMain.on('updateQuitAndInstall', (event) => {
event.returnValue = "ok"
// 关闭所有子窗口
willQuitApp = true
childWindow.some(({browser}) => {
browser && browser.destroy()
})
// 启动更新子窗口
createUpdaterWindow()
// 退出并安装更新
setTimeout(_ => {
autoUpdater.quitAndInstall(true, true)
}, 1)
}, 1000)
})
//================================================================

View File

@ -71,6 +71,7 @@
"files": [
"render/**/*",
"public/**/*",
"updater/**/*",
"electron-menu.js",
"electron-preload.js",
"electron.js",