Skip to content

Commit

Permalink
feat(update): add cancellation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Asuka committed Dec 18, 2024
1 parent 7d5a02e commit 72d5402
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
13 changes: 11 additions & 2 deletions apps/desktop/src/main/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SelectSettings } from '../database/schemas/settings'
import type { UpdateEvent } from '../preload/index.d'
import { app, BrowserWindow, ipcMain } from 'electron'
import Store from 'electron-store'
import pkg from 'electron-updater'
import pkg, { CancellationToken } from 'electron-updater'

// FIXME: https://github.com/sindresorhus/electron-store/issues/276
const store = new Store() as any
Expand Down Expand Up @@ -86,6 +86,8 @@ function sendUpdateStatus(updateEvent: UpdateEvent) {
})
}

let downloadCancelToken: CancellationToken | null = null

function useAppUpdater(settings: SelectSettings) {
const version = app.getVersion()
if (store.get('version') !== version) {
Expand All @@ -96,7 +98,14 @@ function useAppUpdater(settings: SelectSettings) {
return await autoUpdater.checkForUpdates()
})
ipcMain.handle('download-update', async () => {
return await autoUpdater.downloadUpdate()
downloadCancelToken = new CancellationToken()
return await autoUpdater.downloadUpdate(downloadCancelToken)
})
ipcMain.handle('cancel-download', () => {
if (downloadCancelToken) {
downloadCancelToken.cancel()
downloadCancelToken = null
}
})
ipcMain.handle('install-update', async () => {
autoUpdater.quitAndInstall()
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/preload/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare global {
onUpdateStatus: (callback: (event: Electron.IpcRendererEvent, updateEvent: UpdateEvent) => void) => void
checkForUpdates: () => Promise<UpdateCheckResult | null>
downloadUpdate: () => Promise<void>
cancelDownload: () => Promise<void>
installUpdate: () => Promise<void>
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const api = {
}),
checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
downloadUpdate: () => ipcRenderer.invoke('download-update'),
cancelDownload: () => ipcRenderer.invoke('cancel-download'),
installUpdate: () => ipcRenderer.invoke('install-update'),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const { downloadProgress, updateDownloaded } = toRefs(props)
function installUpdate() {
window.api.installUpdate()
}
function cancelDownload() {
window.api.cancelDownload()
dialogVisible.value = false
}
</script>

<template>
Expand All @@ -32,7 +37,7 @@ function installUpdate() {
<ElProgress :percentage="updateDownloaded ? 100 : downloadProgress?.percent" />
</div>
<template #footer>
<ElButton v-if="!updateDownloaded" type="danger">
<ElButton v-if="!updateDownloaded" type="danger" @click="cancelDownload">
{{ $t('update.cancel') }}
</ElButton>
<ElButton v-else type="primary" @click="installUpdate">
Expand Down
13 changes: 12 additions & 1 deletion apps/desktop/src/renderer/src/components/update/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ const downloadProgressDialogVisible = ref(false)
const downloadProgress = ref<ProgressInfo | null>(null)
const updateDownloaded = ref(false)
const { t } = useI18n()
window.api.onUpdateStatus((_event, updateEvent) => {
const { status } = updateEvent
if (status === 'update-available') {
if (status === 'update-not-available') {
if (window.forceCheck) {
ElMessage({
message: t('update.noUpdateAvailable'),
type: 'info',
plain: true,
})
window.forceCheck = false
}
} else if (status === 'update-available') {
const ignoreVersion = localStorage.getItem('ignoreVersion')
const { info, releaseNotes } = updateEvent.data
if (!window.forceCheck && ignoreVersion && ignoreVersion === info.version) {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"src/**/*.vue",
"./typed-router.d.ts",
"./auto-imports.d.ts",
"./components.d.ts"
"./components.d.ts",
"../desktop/src/preload/index.d.ts"
],
"exclude": ["src/**/__tests__/*"]
}
7 changes: 7 additions & 0 deletions packages/ui/src/i18n/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ export default {
tr: 'Kur ve Tekrar Başlat',
hu: 'Telepítés és újraindítás',
},
noUpdateAvailable: {
zh: '您当前使用的已经是最新版本,无需更新。',
en: 'You are already on the latest version, no update is necessary.',
ja: '現在使用しているバージョンは最新です。更新の必要はありません。',
tr: 'Kullandığınız sürüm zaten en son sürüm, güncelleme gerekli değil.',
hu: 'A jelenlegi verzió már a legújabb, nincs szükség frissítésre.',
},
}

0 comments on commit 72d5402

Please sign in to comment.