forked from ECNU/ChatECNU-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.js
More file actions
153 lines (133 loc) · 4.08 KB
/
updater.js
File metadata and controls
153 lines (133 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const { autoUpdater } = require('electron-updater');
const { UPDATE_SERVER_URL } = require('./oss-config');
let mainWindow = null;
let isDownloading = false;
// ========== 更新检查时间常量 ==========
const UPDATE_CHECK_TIMEOUT = 15000; // 检查更新超时时间(毫秒)
const UPDATE_CHECK_DEFAULT_DELAY = 3000; // 启动时检查更新的默认延迟(毫秒)
/**
* 初始化更新器
* @param {BrowserWindow} win - 主窗口实例
*/
function initUpdater(win) {
mainWindow = win;
// 设置更新服务器地址(阿里云 OSS)
autoUpdater.setFeedURL({
provider: 'generic',
url: UPDATE_SERVER_URL
});
// 禁用自动下载,手动控制
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.allowPrerelease = true;
// 检查更新时
autoUpdater.on('checking-for-update', () => {
console.log('[Updater] 正在检查更新...');
if (mainWindow) {
mainWindow.webContents.send('checking-for-update');
}
});
// 发现新版本
autoUpdater.on('update-available', (info) => {
console.log('[Updater] 发现新版本:', info.version);
if (mainWindow) {
// 通知前端发现新版本,由前端控制按钮显示为"立即下载"
mainWindow.webContents.send('update-available', info);
}
});
// 没有新版本
autoUpdater.on('update-not-available', (info) => {
console.log('[Updater] 当前已是最新版本');
if (mainWindow) {
// 通知前端无更新,loading ring 结束
mainWindow.webContents.send('update-not-available');
}
});
// 下载进度
autoUpdater.on('download-progress', (progress) => {
// 通知前端下载进度
if (mainWindow) {
mainWindow.webContents.send('update-download-progress', progress.percent);
}
});
// 下载完成
autoUpdater.on('update-downloaded', (info) => {
console.log('[Updater] 更新下载完成');
isDownloading = false;
if (mainWindow) {
// 通知前端下载完成,按钮变为"立即安装"
mainWindow.webContents.send('update-downloaded', info);
}
});
// 更新错误
autoUpdater.on('error', (err) => {
console.error('[Updater] 更新错误:', err);
isDownloading = false;
if (mainWindow) {
// 出错时也通知前端停止 loading
mainWindow.webContents.send('update-error', err.message);
}
});
}
/**
* 检查更新
*/
function checkForUpdates() {
if (isDownloading) return;
console.log('[Updater] 触发检查更新');
// 手动通知前端进入检查状态,确保 UI 立即响应
if (mainWindow) {
mainWindow.webContents.send('checking-for-update');
}
// 设置超时
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('检查更新超时,请稍后重试'));
}, UPDATE_CHECK_TIMEOUT);
});
Promise.race([
autoUpdater.checkForUpdates(),
timeoutPromise
]).catch(err => {
console.error('[Updater] 检查更新失败:', err);
if (mainWindow) {
mainWindow.webContents.send('update-error', err.message);
}
});
}
/**
* 开始下载更新
*/
function downloadUpdate() {
if (isDownloading) return;
isDownloading = true;
console.log('[Updater] 开始下载更新');
autoUpdater.downloadUpdate().catch(err => {
isDownloading = false;
console.error('[Updater] 下载失败:', err);
if (mainWindow) mainWindow.webContents.send('update-error', err.message);
});
}
/**
* 退出并安装
*/
function quitAndInstall() {
console.log('[Updater] 退出并安装');
autoUpdater.quitAndInstall();
}
/**
* 启动时自动检查更新(延迟执行)
* @param {number} delay - 延迟时间(毫秒),默认 UPDATE_CHECK_DEFAULT_DELAY
*/
function autoCheckOnStartup(delay = UPDATE_CHECK_DEFAULT_DELAY) {
setTimeout(() => {
checkForUpdates();
}, delay);
}
module.exports = {
initUpdater,
checkForUpdates,
downloadUpdate,
quitAndInstall,
autoCheckOnStartup
};