Skip to content

Commit

Permalink
feat: improve update behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Aug 5, 2024
1 parent e1f6caa commit d32c579
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
12 changes: 9 additions & 3 deletions electron/src/main/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -134,18 +134,24 @@ 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 => {
ipcMain.once('destroyed', resolve)
setTimeout(resolve, 5000).unref?.()
})
this.destroyed = true
if (!this.updater.install()) app.quit()
if (!this.updater.install(forceRunAfter)) app.quit()
}
}
21 changes: 12 additions & 9 deletions electron/src/main/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@ 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)
})
autoUpdater.on('update-downloaded', () => {
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
}
Expand Down

0 comments on commit d32c579

Please sign in to comment.