Skip to content

Commit 2e156b8

Browse files
authored
fix(desktop): avoid relaunching without installing updates (#23806)
1 parent 3bfe6a1 commit 2e156b8

7 files changed

Lines changed: 34 additions & 19 deletions

File tree

packages/app/src/components/settings-general.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,12 @@ export const SettingsGeneral: Component = () => {
129129
}
130130

131131
const actions =
132-
platform.update && platform.restart
132+
platform.updateAndRestart
133133
? [
134134
{
135135
label: language.t("toast.update.action.installRestart"),
136136
onClick: async () => {
137-
await platform.update!()
138-
await platform.restart!()
137+
await platform.updateAndRestart!()
139138
},
140139
},
141140
{

packages/app/src/context/platform.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export type Platform = {
4949
/** Storage mechanism, defaults to localStorage */
5050
storage?: (name?: string) => SyncStorage | AsyncStorage
5151

52-
/** Check for updates (Tauri only) */
52+
/** Check for a downloadable desktop update */
5353
checkUpdate?(): Promise<UpdateInfo>
5454

55-
/** Install updates (Tauri only) */
56-
update?(): Promise<void>
55+
/** Install the downloaded update using the platform restart flow */
56+
updateAndRestart?(): Promise<void>
5757

5858
/** Fetch override */
5959
fetch?: typeof fetch

packages/app/src/pages/error.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,9 @@ export const ErrorPage: Component<ErrorPageProps> = (props) => {
244244
}
245245

246246
async function installUpdate() {
247-
if (!platform.update || !platform.restart) return
247+
if (!platform.updateAndRestart) return
248248
await platform
249-
.update()
250-
.then(() => platform.restart!())
249+
.updateAndRestart()
251250
.then(() => setStore("actionError", undefined))
252251
.catch((err) => {
253252
setStore("actionError", formatError(err, language.t))

packages/app/src/pages/layout.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ export default function Layout(props: ParentProps) {
366366

367367
const useUpdatePolling = () =>
368368
onMount(() => {
369-
if (!platform.checkUpdate || !platform.update || !platform.restart) return
369+
if (!platform.checkUpdate || !platform.updateAndRestart) return
370370

371371
let toastId: number | undefined
372372
let interval: ReturnType<typeof setInterval> | undefined
@@ -384,8 +384,7 @@ export default function Layout(props: ParentProps) {
384384
{
385385
label: language.t("toast.update.action.installRestart"),
386386
onClick: async () => {
387-
await platform.update!()
388-
await platform.restart!()
387+
await platform.updateAndRestart!()
389388
},
390389
},
391390
{

packages/desktop-electron/src/main/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,16 @@ function setupAutoUpdater() {
337337
})
338338
}
339339

340-
let updateReady = false
340+
let downloadedUpdateVersion: string | undefined
341341

342342
async function checkUpdate() {
343343
if (!UPDATER_ENABLED) return { updateAvailable: false }
344-
updateReady = false
344+
if (downloadedUpdateVersion) {
345+
logger.log("returning cached downloaded update", {
346+
version: downloadedUpdateVersion,
347+
})
348+
return { updateAvailable: true, version: downloadedUpdateVersion }
349+
}
345350
logger.log("checking for updates", {
346351
currentVersion: app.getVersion(),
347352
channel: autoUpdater.channel,
@@ -367,7 +372,7 @@ async function checkUpdate() {
367372
logger.log("update available", { version })
368373
await autoUpdater.downloadUpdate()
369374
logger.log("update download completed", { version })
370-
updateReady = true
375+
downloadedUpdateVersion = version
371376
return { updateAvailable: true, version }
372377
} catch (error) {
373378
logger.error("update check failed", error)
@@ -376,7 +381,15 @@ async function checkUpdate() {
376381
}
377382

378383
async function installUpdate() {
379-
if (!updateReady) return
384+
if (!downloadedUpdateVersion) {
385+
logger.log("install update skipped", {
386+
reason: "no downloaded update ready",
387+
})
388+
return
389+
}
390+
logger.log("installing downloaded update", {
391+
version: downloadedUpdateVersion,
392+
})
380393
killSidecar()
381394
autoUpdater.quitAndInstall()
382395
}

packages/desktop-electron/src/renderer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const createPlatform = (): Platform => {
170170
return window.api.checkUpdate()
171171
},
172172

173-
update: async () => {
173+
updateAndRestart: async () => {
174174
const config = await window.api.getWindowConfig().catch(() => ({ updaterEnabled: false }))
175175
if (!config.updaterEnabled) return
176176
await window.api.installUpdate()

packages/desktop/src/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,15 @@ const createPlatform = (): Platform => {
297297
return { updateAvailable: true, version: next.version }
298298
},
299299

300-
update: async () => {
300+
updateAndRestart: async () => {
301301
if (!UPDATER_ENABLED || !update) return
302302
if (ostype() === "windows") await commands.killSidecar().catch(() => undefined)
303-
await update.install().catch(() => undefined)
303+
const installed = await update
304+
.install()
305+
.then(() => true)
306+
.catch(() => false)
307+
if (!installed) return
308+
await relaunch()
304309
},
305310

306311
restart: async () => {

0 commit comments

Comments
 (0)