Skip to content

Commit efe1413

Browse files
committed
fix: install update on exit
1 parent d9223e5 commit efe1413

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

apps/desktop/src/electron/autoUpdate.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { autoUpdater, BrowserWindow } from 'electron';
22
import log from 'electron-log/main';
33
import packageJson from '../../package.json';
4+
import { mainStorage } from './storageService';
45

56
export class AutoUpdateManager {
7+
public static versionDownloadedKey = 'versionDownloaded';
8+
69
private readonly channel = 'stable';
710

811
private newAvailableVersion: string | undefined = undefined;
@@ -12,19 +15,37 @@ export class AutoUpdateManager {
1215
// private feedBaseUrl = 'https://update.electronjs.org';
1316
private feedBaseUrl = 'https://tonkeeper-web-updater-test.nkuznetsov.workers.dev';
1417

18+
public static async quitAndInstallIfFlagged() {
19+
const flagged = await mainStorage.get(AutoUpdateManager.versionDownloadedKey);
20+
if (flagged) {
21+
return AutoUpdateManager.quitAndInstall();
22+
}
23+
}
24+
25+
public static async quitAndInstall() {
26+
await mainStorage.delete(AutoUpdateManager.versionDownloadedKey);
27+
setImmediate(() => autoUpdater.quitAndInstall());
28+
return true;
29+
}
30+
1531
constructor(win: BrowserWindow) {
1632
this.win = win;
1733

1834
this.init();
1935
}
2036

21-
private init() {
37+
private async init() {
2238
const feedURL = `${this.feedBaseUrl}/${this.getRepoUrl()}/${process.platform}/${
2339
packageJson.version
2440
}/${this.channel}`;
2541
autoUpdater.setFeedURL({ url: feedURL });
2642
this.listenDownload();
2743

44+
const exited = await AutoUpdateManager.quitAndInstallIfFlagged();
45+
if (exited) {
46+
return;
47+
}
48+
2849
autoUpdater.checkForUpdates();
2950
setInterval(() => {
3051
autoUpdater.checkForUpdates();
@@ -39,10 +60,6 @@ export class AutoUpdateManager {
3960
.trim();
4061
}
4162

42-
public quitAndInstall() {
43-
setImmediate(() => autoUpdater.quitAndInstall());
44-
}
45-
4663
public getNewVersionAvailable() {
4764
return this.newAvailableVersion;
4865
}
@@ -52,6 +69,7 @@ export class AutoUpdateManager {
5269
const version = process.platform === 'win32' ? releaseNotes : releaseName;
5370
this.newAvailableVersion = version;
5471
this.win.webContents.send('app-update::ready', { version });
72+
mainStorage.set(AutoUpdateManager.versionDownloadedKey, version);
5573
log.log('[AutoUpdate] updater new version fetched:', version);
5674
});
5775

apps/desktop/src/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
setProtocolHandlerWindowsLinux
99
} from './electron/protocol';
1010
import { tonConnectSSE } from './electron/sseEvetns';
11+
import { AutoUpdateManager } from './electron/autoUpdate';
1112

1213
app.setName('Tonkeeper Pro');
1314

@@ -42,14 +43,27 @@ if (process.platform !== 'linux') {
4243
}
4344

4445
app.on('before-quit', async e => {
45-
e.preventDefault();
46-
tonConnectSSE.destroy();
47-
if (process.platform !== 'linux') {
48-
powerMonitor.off('unlock-screen', onUnLock);
46+
try {
47+
e.preventDefault();
48+
tonConnectSSE.destroy();
49+
if (process.platform !== 'linux') {
50+
powerMonitor.off('unlock-screen', onUnLock);
51+
}
52+
} catch (e) {
53+
console.error(e);
4954
}
5055

5156
await delay(100);
52-
app.exit();
57+
58+
try {
59+
const exited = AutoUpdateManager.quitAndInstallIfFlagged();
60+
if (!exited) {
61+
app.exit();
62+
}
63+
} catch (e) {
64+
console.error(e);
65+
app.exit();
66+
}
5367
});
5468

5569
setDefaultProtocolClient();

0 commit comments

Comments
 (0)