From ae558a80599c7350f2b64c104f3e99f64101f446 Mon Sep 17 00:00:00 2001 From: purocean Date: Sun, 11 Aug 2019 00:25:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9B=B4=E6=96=B0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E8=BF=9B=E5=BA=A6=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/updater.ts | 76 ++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/app/src/updater.ts b/app/src/updater.ts index 0765d30d..d21bdc72 100644 --- a/app/src/updater.ts +++ b/app/src/updater.ts @@ -11,6 +11,39 @@ const store = new Store() const checkForUpdatesAvailable = app.isPackaged +let progressBar: any = null + +autoUpdater.on('error', e => { + progressBar && (progressBar.detail = '下载失败: ' + e) +}) + +autoUpdater.on('download-progress', e => { + if (progressBar) { + progressBar.value = e.percent + progressBar.detail = '下载中…… ' + e.percent.toFixed(2) + '%' + } +}) + +autoUpdater.on('update-downloaded', () => { + progressBar && progressBar.close() + + dialog.showMessageBox({ + cancelId: 999, + type: 'question', + buttons: ['立即安装', '推迟'], + defaultId: 0, + message: '新版本下载完成,是否要立即安装?' + }).then(result => { + if (result.response === 0) { + setImmediate(() => { + autoUpdater.quitAndInstall() + }) + } + }) +}) + +let cancellationToken: CancellationToken = null + const init = () => { autoUpdater.setFeedURL({ provider: 'github', owner: 'purocean', repo: 'yn'}) autoUpdater.autoDownload = false @@ -25,10 +58,11 @@ const init = () => { }) if (response === 0) { - const progressBar = new ProgressBar({ + progressBar = new ProgressBar({ title: '下载更新', text: `${info.version}`, detail: '正在下载新版本 ', + indeterminate: false, browserWindow: { closable: true, webPreferences: { @@ -37,9 +71,9 @@ const init = () => { } }) - const cancellationToken = new CancellationToken() + cancellationToken = new CancellationToken() autoUpdater.downloadUpdate(cancellationToken).catch(e => { - progressBar.close() + progressBar && progressBar.close() if (e.message !== 'Cancelled') { dialog.showMessageBox({ type: 'info', @@ -53,42 +87,6 @@ const init = () => { console.log('cancel download') cancellationToken.cancel() }) - - const reportUpdateError = (e: any) => { - progressBar.detail = '下载失败: ' + e - } - - autoUpdater.once('error', e => { - if (progressBar._window != null) { - reportUpdateError(e) - } else { - progressBar.on('ready', () => { - reportUpdateError(e) - }) - } - }) - - autoUpdater.once('download-progress', (d) => { - progressBar.value = d.percent - }) - - autoUpdater.once('update-downloaded', () => { - progressBar.close() - - dialog.showMessageBox({ - cancelId: 999, - type: 'question', - buttons: ['立即安装', '推迟'], - defaultId: 0, - message: '新版本下载完成,是否要立即安装?' - }).then(result => { - if (result.response === 0) { - setImmediate(() => { - autoUpdater.quitAndInstall() - }) - } - }) - }) } else if (response === 2) { store.set('dontCheckUpdates', true) }