Skip to content

Commit 41bf060

Browse files
authored
feat: app updater improvements @avivm (#667)
* feat: app updater improvements * docs(changeset): Improved app updater mechanism
1 parent 58f90d9 commit 41bf060

File tree

14 files changed

+221
-167
lines changed

14 files changed

+221
-167
lines changed

.changeset/orange-planets-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@krud-dev/ostara-main": minor
3+
---
4+
5+
Improved app updater mechanism

app/src/infra/autoupdate/appUpdater.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { scheduleJob } from 'node-schedule';
88
import semverGt from 'semver/functions/gt';
99

1010
export class AppUpdater {
11-
constructor(autoUpdate = false) {
11+
constructor() {
1212
if (process.env.NODE_ENV === 'development') {
1313
autoUpdater.updateConfigPath = path.join(__dirname, 'app-dev-update.yml');
1414
}
15-
this.updateAutoUpdate(autoUpdate);
15+
this.updateAutoUpdate(false);
1616
log.transports.file.level = 'info';
1717

1818
autoUpdater.logger = log;
@@ -70,8 +70,8 @@ export class AppUpdater {
7070
return undefined;
7171
}
7272

73-
downloadUpdate() {
74-
autoUpdater.downloadUpdate();
73+
async downloadUpdate(): Promise<void> {
74+
await autoUpdater.downloadUpdate();
7575
}
7676

7777
quitAndInstall() {
@@ -120,10 +120,4 @@ export function initializeAppUpdaterSubscriptions(window: BrowserWindow) {
120120
});
121121
}
122122

123-
configurationStore.onDidChange('autoUpdateEnabled', (newValue) => {
124-
if (newValue !== undefined) {
125-
appUpdater.updateAutoUpdate(newValue);
126-
}
127-
});
128-
129-
export const appUpdater = new AppUpdater(configurationStore.get('autoUpdateEnabled'));
123+
export const appUpdater = new AppUpdater();

app/src/infra/autoupdate/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { ipcMain } from 'electron';
22
import { appUpdater } from './appUpdater';
33

44
ipcMain.handle('appUpdater:checkForUpdates', () => appUpdater.checkForUpdates());
5-
ipcMain.on('appUpdater:downloadUpdate', () => appUpdater.downloadUpdate());
5+
ipcMain.handle('appUpdater:downloadUpdate', () => appUpdater.downloadUpdate());
66
ipcMain.on('appUpdater:quitAndInstall', () => appUpdater.quitAndInstall());

app/src/infra/autoupdate/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { ipcRenderer } from 'electron';
22

33
export const appUpdaterBridge: AppUpdaterBridge = {
44
checkForUpdates: () => ipcRenderer.invoke('appUpdater:checkForUpdates'),
5-
downloadUpdate: () => ipcRenderer.send('appUpdater:downloadUpdate'),
5+
downloadUpdate: () => ipcRenderer.invoke('appUpdater:downloadUpdate'),
66
quitAndInstall: () => ipcRenderer.send('appUpdater:quitAndInstall'),
77
};

app/src/infra/autoupdate/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { UpdateInfo } from 'electron-updater';
33
declare global {
44
type AppUpdaterBridge = {
55
checkForUpdates(): Promise<UpdateInfo | undefined>;
6-
downloadUpdate(): void;
6+
downloadUpdate(): Promise<void>;
77
quitAndInstall(): void;
88
};
99

app/src/infra/store/renderer.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,4 @@ export const configurationStoreBridge: ConfigurationBridge<keyof Configuration>
2626
setErrorReportingEnabled(enabled: boolean): void {
2727
ipcRenderer.send('configurationStore:set', 'errorReportingEnabled', enabled);
2828
},
29-
isAutoUpdateEnabled(): boolean {
30-
return ipcRenderer.sendSync('configurationStore:get', 'autoUpdateEnabled');
31-
},
32-
setAutoUpdateEnabled(enabled: boolean): void {
33-
ipcRenderer.send('configurationStore:set', 'autoUpdateEnabled', enabled);
34-
},
3529
};

app/src/infra/store/store.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ export const defaults = {
55
* Whether Sentry error reporting is enabled or not.
66
*/
77
errorReportingEnabled: true,
8-
/**
9-
* Whether auto-updates are enabled or not.
10-
*/
11-
autoUpdateEnabled: true,
128

139
lastUpdateCheckTime: 0,
1410
};

app/src/infra/store/types.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ declare global {
1010
clear(): void;
1111
isErrorReportingEnabled(): boolean;
1212
setErrorReportingEnabled(enabled: boolean): void;
13-
14-
isAutoUpdateEnabled(): boolean;
15-
16-
setAutoUpdateEnabled(enabled: boolean): void;
1713
};
1814

1915
interface Window {

app/src/renderer/apis/requests/crud/entity/entity.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type CrudEntityType = 'CrudFramework' | 'LocalStorage';
1+
export type CrudEntityType = 'CrudFramework' | 'Stub';
22

33
export type CrudEntityBase = {
44
id: string;

app/src/renderer/components/managers/AppUpdatesManager.tsx

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import { notEmpty } from '../../utils/objectUtils';
1111
import { useLocalStorageState } from '../../hooks/useLocalStorageState';
1212
import { useAppUpdatesContext } from '../../contexts/AppUpdatesContext';
1313
import { useSnackbar } from 'notistack';
14-
import semverGte from 'semver/functions/gte';
1514

1615
interface AppUpdatesManagerProps {}
1716

1817
const AppUpdatesManager: FunctionComponent<AppUpdatesManagerProps> = () => {
19-
const { autoUpdateSupported, autoUpdateEnabled, newVersionDownloaded, newVersionInfo } = useAppUpdatesContext();
18+
const { autoUpdateSupported, newVersionDownloaded, newVersionInfo } = useAppUpdatesContext();
2019

2120
const [newVersionDetailsShown, setNewVersionDetailsShown] = useState<string | undefined>(undefined);
2221
const [newVersionDetailsSkipVersion, setNewVersionDetailsSkipVersion] = useLocalStorageState<string | undefined>(
@@ -26,51 +25,41 @@ const AppUpdatesManager: FunctionComponent<AppUpdatesManagerProps> = () => {
2625
const [newVersionDownloadedShown, setNewVersionDownloadedShown] = useState<string | undefined>(undefined);
2726

2827
useEffect(() => {
29-
(async () => {
30-
if (!newVersionInfo) {
31-
return;
32-
}
33-
const appVersion = await window.ui.getAppVersion();
34-
if (semverGte(appVersion, newVersionInfo.version)) {
35-
return;
36-
}
37-
if (newVersionDetailsShown === newVersionInfo.version) {
38-
return;
39-
}
40-
if (newVersionDetailsSkipVersion === newVersionInfo.version) {
41-
return;
42-
}
43-
if (autoUpdateSupported && (autoUpdateEnabled || newVersionDownloaded)) {
44-
return;
45-
}
46-
setNewVersionDetailsShown(newVersionInfo.version);
47-
NiceModal.show<undefined, AppUpdateDetailsDialogProps>(AppUpdateDetailsDialog, {
48-
updateInfo: newVersionInfo,
49-
onSkipVersion: setNewVersionDetailsSkipVersion,
50-
});
51-
})();
28+
if (!newVersionInfo) {
29+
return;
30+
}
31+
if (newVersionDetailsShown === newVersionInfo.version) {
32+
return;
33+
}
34+
if (newVersionDetailsSkipVersion === newVersionInfo.version) {
35+
return;
36+
}
37+
if (autoUpdateSupported && newVersionDownloaded) {
38+
return;
39+
}
40+
41+
setNewVersionDetailsShown(newVersionInfo.version);
42+
NiceModal.show<undefined, AppUpdateDetailsDialogProps>(AppUpdateDetailsDialog, {
43+
updateInfo: newVersionInfo,
44+
onSkipVersion: setNewVersionDetailsSkipVersion,
45+
});
5246
}, [newVersionInfo]);
5347

5448
useEffect(() => {
55-
(async () => {
56-
if (!newVersionDownloaded) {
57-
return;
58-
}
59-
const appVersion = await window.ui.getAppVersion();
60-
if (semverGte(appVersion, newVersionDownloaded.version)) {
61-
return;
62-
}
63-
if (newVersionDownloadedShown === newVersionDownloaded.version) {
64-
return;
65-
}
66-
if (!autoUpdateSupported) {
67-
return;
68-
}
69-
setNewVersionDownloadedShown(newVersionDownloaded.version);
70-
NiceModal.show<undefined, AppUpdateDownloadedDialogProps>(AppUpdateDownloadedDialog, {
71-
updateInfo: newVersionDownloaded,
72-
});
73-
})();
49+
if (!newVersionDownloaded) {
50+
return;
51+
}
52+
if (newVersionDownloadedShown === newVersionDownloaded.version) {
53+
return;
54+
}
55+
if (!autoUpdateSupported) {
56+
return;
57+
}
58+
59+
setNewVersionDownloadedShown(newVersionDownloaded.version);
60+
NiceModal.show<undefined, AppUpdateDownloadedDialogProps>(AppUpdateDownloadedDialog, {
61+
updateInfo: newVersionDownloaded,
62+
});
7463
}, [newVersionDownloaded]);
7564

7665
return null;
@@ -87,7 +76,7 @@ const AppUpdateDetailsDialog: FunctionComponent<AppUpdateDetailsDialogProps> = N
8776
({ updateInfo, onSkipVersion }) => {
8877
const modal = useModal();
8978
const { track } = useAnalyticsContext();
90-
const { downloadUpdate } = useAppUpdatesContext();
79+
const { autoUpdateSupported, downloadUpdate } = useAppUpdatesContext();
9180
const { enqueueSnackbar } = useSnackbar();
9281

9382
const closeHandler = useCallback((): void => {
@@ -116,13 +105,14 @@ const AppUpdateDetailsDialog: FunctionComponent<AppUpdateDetailsDialogProps> = N
116105
const downloadHandler = useCallback((): void => {
117106
track({ name: 'app_update_details_dialog_download' });
118107

119-
const downloadType = downloadUpdate();
120-
if (downloadType === 'internal') {
108+
downloadUpdate();
109+
110+
if (autoUpdateSupported) {
121111
enqueueSnackbar(<FormattedMessage id="downloadStarted" />, { variant: 'info' });
122112
}
123113

124114
closeHandler();
125-
}, [downloadUpdate, closeHandler]);
115+
}, [autoUpdateSupported, downloadUpdate, closeHandler]);
126116

127117
const laterHandler = useCallback((): void => {
128118
track({ name: 'app_update_details_dialog_later' });

0 commit comments

Comments
 (0)