11import { type AppUpdater , autoUpdater } from "electron-updater" ;
22import log from "./logger" ;
3- import { BrowserWindow } from "electron" ;
43import { MainWindowNotifier } from "./types" ;
54
65type AutoUpdateStatus = {
@@ -12,6 +11,7 @@ autoUpdater.logger = log;
1211
1312let mainWindowNotifier : MainWindowNotifier | null = null ;
1413let initalized = false ;
14+ let isUpdating = false ;
1515
1616// keep the last status to resend to the window when it's opened becuase the store is destroyed when the window is closed
1717let lastStatus : AutoUpdateStatus = { status : "up-to-date" } ;
@@ -21,13 +21,13 @@ export const getUpdater = () => autoUpdater;
2121export function checkUpdater ( notifier : MainWindowNotifier ) : AppUpdater {
2222 try {
2323 mainWindowNotifier = notifier ;
24- autoUpdater . checkForUpdatesAndNotify ( ) ;
24+ checkForUpdates ( ) ;
2525
2626 if ( ! initalized ) {
2727 initalized = true ;
2828
2929 setInterval ( ( ) => {
30- autoUpdater . checkForUpdatesAndNotify ( ) ;
30+ checkForUpdates ( ) ;
3131 } , 1000 * 60 * 30 ) ; // check for updates every 30 minutes
3232 }
3333 } catch ( error ) {
@@ -37,6 +37,30 @@ export function checkUpdater(notifier: MainWindowNotifier): AppUpdater {
3737 return autoUpdater ;
3838}
3939
40+ const checkForUpdates = async ( ) => {
41+ if ( isUpdating ) {
42+ log . info ( "Update is already in progress" ) ;
43+ return ;
44+ }
45+
46+ isUpdating = true ;
47+
48+ try {
49+ const result = await autoUpdater . checkForUpdatesAndNotify ( ) ;
50+ if ( ! result ?. downloadPromise ) {
51+ isUpdating = false ;
52+ } else {
53+ const files = await result . downloadPromise ;
54+ log . info ( "Successfully downloaded update files:" , files ) ;
55+ // DO NOT RESET isUpdating here because the user still needs to click to install it
56+ // and we don't want to accidentally start another update and overwrite the file
57+ }
58+ } catch ( err ) {
59+ log . error ( "Error checking for updates:" , err ) ;
60+ isUpdating = false ;
61+ }
62+ } ;
63+
4064// The auto update runs in the background so the window might not be open when the status changes
4165// When the update store gets created as part of the window it will request the latest status.
4266export function getAutoUpdateStatus ( ) {
0 commit comments