feat: improve update behavior

This commit is contained in:
ThaUnknown 2024-08-05 23:42:23 +02:00 committed by NoCrypt
parent 073d71798d
commit f57a7d34f4
2 changed files with 21 additions and 12 deletions

View file

@ -48,7 +48,7 @@ export default class App {
discord = new Discord(this.mainWindow)
protocol = new Protocol(this.mainWindow)
updater = new Updater(this.mainWindow)
updater = new Updater(this.mainWindow, this.webtorrentWindow)
dialog = new Dialog(this.webtorrentWindow)
constructor () {
@ -134,11 +134,17 @@ export default class App {
this.webtorrentWindow.webContents.postMessage('torrentPath', store.get('torrentPath'))
sender.postMessage('port', null, [port2])
})
ipcMain.on('quit-and-install', () => {
if (this.updater.hasUpdate) {
this.destroy(true)
}
})
}
destroyed = false
async destroy () {
async destroy (forceRunAfter = false) {
if (this.destroyed) return
this.webtorrentWindow.webContents.postMessage('destroy', null)
await new Promise(resolve => {
@ -146,6 +152,6 @@ export default class App {
setTimeout(resolve, 5000).unref?.()
})
this.destroyed = true
if (!this.updater.install()) app.quit()
if (!this.updater.install(forceRunAfter)) app.quit()
}
}

View file

@ -12,10 +12,15 @@ ipcMain.on('update', () => {
// autoUpdater.checkForUpdatesAndNotify()
export default class Updater {
hasUpdate = false
window
torrentWindow
/**
* @param {import('electron').BrowserWindow} window
* @param {import('electron').BrowserWindow} torrentWindow
*/
constructor (window) {
constructor (window, torrentWindow) {
this.window = window
this.torrentWindow = torrentWindow
autoUpdater.on('update-available', () => {
window.webContents.send('update-available', true)
})
@ -23,17 +28,15 @@ export default class Updater {
this.hasUpdate = true
window.webContents.send('update-downloaded', true)
})
ipcMain.on('quit-and-install', () => {
if (this.hasUpdate) {
autoUpdater.quitAndInstall()
this.hasUpdate = false
}
})
}
install () {
install (forceRunAfter = false) {
if (this.hasUpdate) {
autoUpdater.quitAndInstall()
setImmediate(() => {
this.window.close()
this.torrentWindow.close()
autoUpdater.quitAndInstall(true, forceRunAfter)
})
this.hasUpdate = false
return true
}