Updater.svelte 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <script>
  2. import { onMount, onDestroy } from "svelte";
  3. // This example show how updater events work when dialog is disabled.
  4. // This allow you to use custom dialog for the updater.
  5. // This is your responsability to restart the application after you receive the STATUS: DONE.
  6. import { checkUpdate, installUpdate } from "@tauri-apps/api/updater";
  7. import { listen } from "@tauri-apps/api/event";
  8. import { relaunch } from "@tauri-apps/api/app";
  9. export let onMessage;
  10. let unlisten;
  11. onMount(async () => {
  12. unlisten = await listen("tauri://update-status", onMessage)
  13. })
  14. onDestroy(() => {
  15. if (unlisten) {
  16. unlisten()
  17. }
  18. })
  19. async function check() {
  20. try {
  21. document.getElementById("check_update").classList.add("hidden");
  22. const {shouldUpdate, manifest} = await checkUpdate();
  23. onMessage(`Should update: ${shouldUpdate}`);
  24. onMessage(manifest);
  25. if (shouldUpdate) {
  26. document.getElementById("start_update").classList.remove("hidden");
  27. }
  28. } catch(e) {
  29. onMessage(e);
  30. }
  31. }
  32. async function install() {
  33. try {
  34. document.getElementById("start_update").classList.add("hidden");
  35. await installUpdate();
  36. onMessage("Installation complete, restart required.");
  37. await relaunch();
  38. } catch(e) {
  39. onMessage(e);
  40. }
  41. }
  42. </script>
  43. <div>
  44. <button class="button" id="check_update" on:click={check}>Check update</button>
  45. <button class="button hidden" id="start_update" on:click={install}>Install update</button>
  46. </div>