|
1 | | -<script> |
2 | | - import { check } from '@tauri-apps/plugin-updater' |
| 1 | +<script lang="ts"> |
| 2 | + import { check, Update } from '@tauri-apps/plugin-updater' |
3 | 3 | import { relaunch } from '@tauri-apps/plugin-process' |
| 4 | + import { onDestroy } from 'svelte' |
4 | 5 |
|
5 | | - export let onMessage |
| 6 | + let { onMessage } = $props() |
6 | 7 |
|
7 | | - let isChecking, isInstalling, newUpdate |
8 | | - let totalSize = 0, |
9 | | - downloadedSize = 0 |
| 8 | + let isChecking = $state(false) |
| 9 | + let isInstalling = $state(false) |
| 10 | + let newUpdate = $state<Update | undefined>() |
| 11 | + let totalSize = $state(0) |
| 12 | + let downloadedSize = $state(0) |
| 13 | + let progress = $derived( |
| 14 | + totalSize ? Math.round((downloadedSize / totalSize) * 100) : 0 |
| 15 | + ) |
10 | 16 |
|
11 | 17 | async function checkUpdate() { |
12 | 18 | isChecking = true |
|
31 | 37 | isInstalling = true |
32 | 38 | downloadedSize = 0 |
33 | 39 | try { |
34 | | - await newUpdate.downloadAndInstall((downloadProgress) => { |
| 40 | + await newUpdate!.downloadAndInstall((downloadProgress) => { |
35 | 41 | switch (downloadProgress.event) { |
36 | 42 | case 'Started': |
37 | | - totalSize = downloadProgress.data.contentLength |
| 43 | + totalSize = downloadProgress.data.contentLength! |
38 | 44 | break |
39 | 45 | case 'Progress': |
40 | 46 | downloadedSize += downloadProgress.data.chunkLength |
|
54 | 60 | } |
55 | 61 | } |
56 | 62 |
|
57 | | - $: progress = totalSize ? Math.round((downloadedSize / totalSize) * 100) : 0 |
| 63 | + onDestroy(() => { |
| 64 | + newUpdate?.close() |
| 65 | + }) |
58 | 66 | </script> |
59 | 67 |
|
60 | 68 | <div class="flex children:grow children:h10"> |
61 | 69 | {#if !isChecking && !newUpdate} |
62 | | - <button class="btn" on:click={checkUpdate}>Check update</button> |
| 70 | + <button class="btn" onclick={checkUpdate}>Check update</button> |
63 | 71 | {:else if !isInstalling && newUpdate} |
64 | | - <button class="btn" on:click={install}>Install update</button> |
| 72 | + <button class="btn" onclick={install}>Install update</button> |
65 | 73 | {:else} |
66 | 74 | <div class="progress"> |
67 | 75 | <span>{progress}%</span> |
|
0 commit comments