update download + apply flow

This commit is contained in:
2026-05-12 01:59:39 +03:00
parent 953263028b
commit 61fbe68251
4 changed files with 233 additions and 16 deletions
+96 -15
View File
@@ -1,25 +1,69 @@
<script lang="ts">
import { onMount } from "svelte"
import { api, type UpdateInfo } from "./api"
import { api, formatBytes, type UpdateInfo, type UpdateDownloadProgress } from "./api"
type Phase = "info" | "downloading" | "ready-to-apply" | "error"
let info: UpdateInfo | null = $state(null)
let phase: Phase = $state("info")
let progress: UpdateDownloadProgress | null = $state(null)
let errMsg: string | null = $state(null)
onMount(() => {
const unlistenPromise = api.onUpdateAvailable((i) => {
info = i
})
let unlistenInfo: (() => void) | null = null
let unlistenProgress: (() => void) | null = null
void (async () => {
unlistenInfo = await api.onUpdateAvailable((i) => {
info = i
phase = "info"
})
unlistenProgress = await api.onUpdateDownloadProgress((p) => {
progress = p
})
if (await api.pendingUpdate()) {
phase = "ready-to-apply"
}
})()
return () => {
unlistenPromise.then((fn) => fn())
unlistenInfo?.()
unlistenProgress?.()
}
})
function dismiss() {
info = null
phase = "info"
errMsg = null
}
function viewRelease() {
if (info) {
void api.openUrl(info.release_url)
if (info) void api.openUrl(info.release_url)
}
async function download() {
if (!info?.download_url) {
errMsg = "this release has no downloadable exe asset"
phase = "error"
return
}
phase = "downloading"
progress = null
errMsg = null
try {
await api.downloadUpdate(info.download_url)
phase = "ready-to-apply"
} catch (e) {
errMsg = String(e)
phase = "error"
}
}
async function apply() {
try {
await api.applyUpdate()
} catch (e) {
errMsg = String(e)
phase = "error"
}
}
@@ -30,19 +74,51 @@
})
</script>
{#if info}
{#if info || phase === "ready-to-apply"}
<div class="banner">
<div class="text">
<strong>update available</strong>
<span class="ver">v{info.version}</span>
<span class="from">(you have v{info.current_version})</span>
{#if bodyPreview}
<p class="body">{bodyPreview}{info.body.length > 140 ? "..." : ""}</p>
{#if phase === "ready-to-apply"}
<strong>update ready to apply</strong>
{#if info}
<span class="ver">v{info.version}</span>
{/if}
<p class="body">click apply to swap and restart, or dismiss to keep the current version.</p>
{:else if phase === "downloading"}
<strong>downloading update...</strong>
{#if info}<span class="ver">v{info.version}</span>{/if}
{#if progress}
<p class="body">
{formatBytes(progress.downloaded)}
{#if progress.total} / {formatBytes(progress.total)}{/if}
</p>
{/if}
{:else if phase === "error"}
<strong>update error</strong>
<p class="body">{errMsg}</p>
{:else if info}
<strong>update available</strong>
<span class="ver">v{info.version}</span>
<span class="from">(you have v{info.current_version})</span>
{#if bodyPreview}
<p class="body">{bodyPreview}{info.body.length > 140 ? "..." : ""}</p>
{/if}
{/if}
</div>
<div class="actions">
<button class="primary" onclick={viewRelease}>view release</button>
<button onclick={dismiss}>dismiss</button>
{#if phase === "ready-to-apply"}
<button class="primary" onclick={apply}>apply now</button>
<button onclick={dismiss}>dismiss</button>
{:else if phase === "downloading"}
<span class="muted">working...</span>
{:else if phase === "error"}
<button onclick={dismiss}>dismiss</button>
{:else if info}
{#if info.download_url}
<button class="primary" onclick={download}>download</button>
{/if}
<button onclick={viewRelease}>view release</button>
<button onclick={dismiss}>dismiss</button>
{/if}
</div>
</div>
{/if}
@@ -83,6 +159,11 @@
display: flex;
gap: 0.4rem;
flex-shrink: 0;
align-items: center;
}
.muted {
color: #7a8e88;
font-size: 0.85rem;
}
button {
font: inherit;
+15
View File
@@ -56,6 +56,12 @@ export type UpdateInfo = {
current_version: string
release_url: string
body: string
download_url: string | null
}
export type UpdateDownloadProgress = {
downloaded: number
total: number | null
}
export type CaptureRect = {
@@ -132,6 +138,15 @@ export const api = {
listen<UpdateInfo>("update-available", (e) => handler(e.payload)),
onHotkey: (handler: (kind: "start" | "stop" | "pause") => void): Promise<UnlistenFn> =>
listen<"start" | "stop" | "pause">("hotkey", (e) => handler(e.payload)),
downloadUpdate: (url: string) => invoke<void>("download_update", { url }),
applyUpdate: () => invoke<void>("apply_update"),
pendingUpdate: () => invoke<boolean>("pending_update"),
onUpdateDownloadProgress: (
handler: (p: UpdateDownloadProgress) => void,
): Promise<UnlistenFn> =>
listen<UpdateDownloadProgress>("update-download-progress", (e) =>
handler(e.payload),
),
}
export function formatBytes(n: number): string {