Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

Commit c3bc247

Browse files
committed
Refactor auto updating
1 parent 9887813 commit c3bc247

File tree

5 files changed

+95
-94
lines changed

5 files changed

+95
-94
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ charset = utf-8
55
end_of_line = lf
66
insert_final_newline = true
77
trim_trailing_whitespace = true
8+
9+
[*.js]
10+
indent_size = 2
11+
indent_style = space

package-lock.json

Lines changed: 12 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"dependencies": {
2525
"axios": "^0.18.1",
2626
"electron-is-dev": "^1.0.1",
27-
"electron-progressbar": "^1.2.0",
2827
"electron-updater": "^4.2.0",
2928
"electron-window-state": "^5.0.3",
3029
"flag-icon-css": "^3.3.0",
Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,91 @@
11
import { dialog } from "electron";
22
import isDev from "electron-is-dev";
33
import { autoUpdater } from "electron-updater";
4-
import ProgressBar from "electron-progressbar";
54

6-
let progressBar = null;
75
let isUpdating = false;
8-
let downloadAndInstall = false;
96

10-
function checkForUpdate(onQuitAndInstall) {
7+
async function checkForUpdate(getMainWindow, onQuitAndInstall) {
118
// Disable for development
129
if (isDev) {
1310
return;
1411
}
1512

16-
autoUpdater.logger = console;
17-
autoUpdater.autoDownload = false;
18-
19-
autoUpdater.on("error", err => {
20-
if (isUpdating) {
21-
dialog.showErrorBox("Update Error: ", err == null ? "unknown" : err.message);
22-
isUpdating = false;
23-
console.error("Error in auto-updater.", err.message);
24-
}
25-
});
26-
27-
autoUpdater.on("update-available", info => {
28-
console.log(`Update available: ${info.version}`);
13+
if (isUpdating) {
14+
return;
15+
}
2916

30-
const message = `Update ${info.version} found. Do you want to download the update?`;
31-
const detail = `View the release notes at: https://github.com/loki-project/loki-electron-gui-wallet/releases/tag/v${info.version}`;
17+
autoUpdater.logger = console;
3218

33-
dialog.showMessageBox(
34-
{
35-
type: "info",
36-
title: "Update available",
37-
message,
38-
detail,
39-
buttons: ["Download and Install", "Download and Install Later", "No"],
40-
defaultId: 0
41-
},
42-
buttonIndex => {
43-
// Download and install
44-
if (buttonIndex === 0) {
45-
downloadAndInstall = true;
46-
if (!progressBar) {
47-
progressBar = new ProgressBar({
48-
indeterminate: false,
49-
title: "Downloading...",
50-
text: `Downloading wallet v${info.version}`
51-
});
52-
}
53-
}
19+
try {
20+
// Get the update using electron-updater
21+
try {
22+
const info = await autoUpdater.checkForUpdates();
23+
if (!info.downloadPromise) {
24+
console.info("auto-update: no update to download");
5425

55-
// Download
56-
if (buttonIndex !== 2) {
57-
isUpdating = true;
58-
autoUpdater.downloadUpdate();
59-
}
26+
return;
6027
}
61-
);
62-
});
63-
64-
autoUpdater.on("download-progress", progress => {
65-
if (progressBar) {
66-
progressBar.value = progress.percent;
28+
await info.downloadPromise;
29+
} catch (error) {
30+
await showCannotUpdateDialog(getMainWindow());
31+
throw error;
6732
}
68-
});
6933

70-
autoUpdater.on("update-downloaded", () => {
71-
console.log("Update downloaded");
72-
isUpdating = false;
73-
74-
if (progressBar) {
75-
progressBar.setCompleted();
76-
progressBar = null;
34+
// Update downloaded successfully, we should ask the user to update
35+
console.info("auto-update: showing update dialog...");
36+
const shouldUpdate = await showUpdateDialog(getMainWindow());
37+
if (!shouldUpdate) {
38+
return;
7739
}
7840

79-
// If download and install was selected then quit and install
80-
if (downloadAndInstall && onQuitAndInstall) {
41+
console.info("auto-update: calling quitAndInstall...");
42+
if (onQuitAndInstall) {
8143
onQuitAndInstall(autoUpdater);
82-
downloadAndInstall = false;
8344
}
45+
} catch (error) {
46+
console.error("auto-update error:", getPrintableError(error));
47+
} finally {
48+
isUpdating = false;
49+
}
50+
}
51+
52+
function getPrintableError(error) {
53+
return error && error.stack ? error.stack : error;
54+
}
55+
56+
async function showUpdateDialog(mainWindow) {
57+
const RESTART_BUTTON = 0;
58+
const LATER_BUTTON = 1;
59+
const options = {
60+
type: "info",
61+
buttons: ["Restart Wallet", "Later"],
62+
title: "Loki Electron Wallet update available",
63+
message: "There is a new version of Loki Electron Wallet available.",
64+
detail: "Press Restart Wallet to apply the update",
65+
defaultId: LATER_BUTTON,
66+
cancelId: RESTART_BUTTON
67+
};
68+
return new Promise(resolve => {
69+
dialog.showMessageBox(mainWindow, options, response => {
70+
resolve(response === RESTART_BUTTON);
71+
});
8472
});
73+
}
8574

86-
autoUpdater.checkForUpdates();
75+
async function showCannotUpdateDialog(mainWindow) {
76+
const options = {
77+
type: "error",
78+
buttons: ["Ok"],
79+
title: "Cannot update",
80+
message:
81+
"Loki Electron Wallet failed to update but there is a new version available. Please go to https://loki.network/ and install the new version manually."
82+
};
83+
84+
return new Promise(resolve => {
85+
dialog.showMessageBox(mainWindow, options, () => {
86+
resolve();
87+
});
88+
});
8789
}
8890

8991
export { checkForUpdate };

src-electron/main-process/electron-main.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,21 @@ function createWindow() {
155155
}
156156

157157
app.on("ready", () => {
158-
checkForUpdate(autoUpdater => {
159-
if (mainWindow) {
160-
mainWindow.webContents.send("showQuitScreen");
161-
}
158+
checkForUpdate(
159+
() => mainWindow,
160+
autoUpdater => {
161+
if (mainWindow) {
162+
mainWindow.webContents.send("showQuitScreen");
163+
}
162164

163-
const promise = backend ? backend.quit() : Promise.resolve();
164-
promise.then(() => {
165-
installUpdate = true;
166-
backend = null;
167-
autoUpdater.quitAndInstall();
168-
});
169-
});
165+
const promise = backend ? backend.quit() : Promise.resolve();
166+
promise.then(() => {
167+
installUpdate = true;
168+
backend = null;
169+
autoUpdater.quitAndInstall();
170+
});
171+
}
172+
);
170173
if (process.platform === "darwin") {
171174
const menu = Menu.buildFromTemplate(menuTemplate);
172175
Menu.setApplicationMenu(menu);

0 commit comments

Comments
 (0)