Skip to content

Commit

Permalink
[Desktop] Possibly fix buggy updater
Browse files Browse the repository at this point in the history
Ref: #178
  • Loading branch information
brunolemos committed Sep 15, 2019
1 parent bc787ff commit f864c0f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
13 changes: 12 additions & 1 deletion packages/desktop/src/index.ts
Expand Up @@ -82,7 +82,18 @@ function init() {
}
})

app.on('window-all-closed', () => {
app.on('window-all-closed', async () => {
if (updater.getUpdateInfo().state === 'update-downloaded') {
try {
const mainWindow = window.getMainWindow()
if (mainWindow) await mainWindow.webContents.session.clearCache()
} catch (error) {
console.error(error)
}
app.quit()
return
}

if (process.platform !== 'darwin') {
app.quit()
}
Expand Down
65 changes: 34 additions & 31 deletions packages/desktop/src/updater.ts
Expand Up @@ -2,6 +2,7 @@ import { app, dialog, Notification } from 'electron'
import { autoUpdater } from 'electron-updater'

import * as menu from './menu'
import * as window from './window'

let updateInfo: {
state:
Expand All @@ -15,6 +16,7 @@ let updateInfo: {
date: number
progress?: number
lastManuallyCheckedAt?: number
version?: string
} = {
state: 'not-checked',
date: Date.now(),
Expand All @@ -36,42 +38,36 @@ export function getAutoUpdater() {
return autoUpdater
}

let shouldNotify = false
export async function checkForUpdatesAndNotify() {
try {
const result = await autoUpdater.checkForUpdates()
if (!(result && result.downloadPromise)) return result

try {
await result.downloadPromise

const version =
result.updateInfo.version && result.updateInfo.version.startsWith('v')
? result.updateInfo.version.slice(1)
: result.updateInfo.version

const notification = new Notification({
title: '🚀 New version available',
body: `${app.getName()} v${version} has been downloaded. Restart the app to get the update.`,
})

notification.addListener('click', () => {
autoUpdater.autoInstallOnAppQuit = true
app.relaunch()
app.quit()
})

notification.show()
} catch (error) {
console.error(error)
}

return result
await autoUpdater.checkForUpdates()
shouldNotify = true
} catch (error) {
console.error(error)
return null
}
}

function notify() {
const version =
updateInfo.version && updateInfo.version.startsWith('v')
? updateInfo.version.slice(1)
: updateInfo.version

const notification = new Notification({
title: version
? `🚀 ${app.getName()} v${version} is now available`
: '🚀 New version available',
body: 'Restart the app to update it.',
})

notification.on('click', e => {
autoUpdater.quitAndInstall()
})

notification.show()
}

export function register() {
autoUpdater.on('error', () => {
updateInfo = { ...updateInfo, state: 'error', date: Date.now() }
Expand Down Expand Up @@ -121,8 +117,15 @@ export function register() {
menu.updateMenu()
})

autoUpdater.on('update-downloaded', () => {
updateInfo = { ...updateInfo, state: 'update-downloaded', date: Date.now() }
autoUpdater.on('update-downloaded', info => {
updateInfo = {
...updateInfo,
state: 'update-downloaded',
date: Date.now(),
version: info && info.version,
}
menu.updateMenu()

if (shouldNotify) notify()
})
}

0 comments on commit f864c0f

Please sign in to comment.