Skip to content

Commit

Permalink
Call JS updater API
Browse files Browse the repository at this point in the history
  • Loading branch information
i-c-b committed Oct 31, 2023
1 parent 4b7504f commit 73b2a94
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "tauri-app",
"version": "1.2.3-6"
"version": "1.2.4-7"
},
"tauri": {
"bundle": {
Expand Down
52 changes: 52 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
const { invoke } = window.__TAURI__.primitives;
const { check } = window.__TAURI__.updater;

let greetInputEl;
let greetMsgEl;
let updateMsgEl;

async function greet() {
await updateIfAvailable();

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
greetMsgEl.textContent = await invoke("greet", { name: greetInputEl.value });
}

window.addEventListener("DOMContentLoaded", () => {
greetInputEl = document.querySelector("#greet-input");
greetMsgEl = document.querySelector("#greet-msg");
updateMsgEl = document.querySelector("#update-msg");
document.querySelector("#greet-form").addEventListener("submit", (e) => {
e.preventDefault();
greet();
});
});

async function updateIfAvailable() {
let update;
try {
update = await check();
} catch (e) {
console.debug("failed check", e);
return;
}

if (update !== null) {
console.debug(`Update available for ${update.currentVersion}, updating to ${update.version}`);

try {
await update.downloadAndInstall(progressHandler);
} catch (e) {
console.debug("failed check", e);
return;
}
} else {
console.debug("Already running latest version");
}
}

function progressHandler(progress) {
switch (progress.event) {
case "Started": {
console.debug("Update event started", progress.data.contentLength);
updateMsgEl.textContent = `Update available of size: ${progress.data.contentLength}`;
break;
}
case "Progress": {
console.debug("Update event progress", progress.data.chunkLength);
updateMsgEl.textContent = `Update download progress: ${progress.data.chunkLength}`;
break;
}
case "Finished": {
console.debug("Update event finished");
updateMsgEl.textContent = "Update finished";
break;
}
default: {
console.debug("Update event unrecognised");
break;
}
}
}

0 comments on commit 73b2a94

Please sign in to comment.