1616using Flow . Launcher . Infrastructure . UserSettings ;
1717using Flow . Launcher . Plugin ;
1818using System . Text . Json . Serialization ;
19+ using System . Threading ;
1920
2021namespace Flow . Launcher . Core
2122{
@@ -28,21 +29,21 @@ public Updater(string gitHubRepository)
2829 GitHubRepository = gitHubRepository ;
2930 }
3031
31- public async Task UpdateApp ( IPublicAPI api , bool silentUpdate = true )
32+ private SemaphoreSlim UpdateLock { get ; } = new SemaphoreSlim ( 1 ) ;
33+
34+ public async Task UpdateAppAsync ( IPublicAPI api , bool silentUpdate = true )
3235 {
36+ await UpdateLock . WaitAsync ( ) ;
3337 try
3438 {
35- UpdateInfo newUpdateInfo ;
36-
3739 if ( ! silentUpdate )
3840 api . ShowMsg ( api . GetTranslation ( "pleaseWait" ) ,
39- api . GetTranslation ( "update_flowlauncher_update_check" ) ) ;
41+ api . GetTranslation ( "update_flowlauncher_update_check" ) ) ;
4042
4143 using var updateManager = await GitHubUpdateManager ( GitHubRepository ) . ConfigureAwait ( false ) ;
4244
43-
4445 // UpdateApp CheckForUpdate will return value only if the app is squirrel installed
45- newUpdateInfo = await updateManager . CheckForUpdate ( ) . NonNull ( ) . ConfigureAwait ( false ) ;
46+ var newUpdateInfo = await updateManager . CheckForUpdate ( ) . NonNull ( ) . ConfigureAwait ( false ) ;
4647
4748 var newReleaseVersion = Version . Parse ( newUpdateInfo . FutureReleaseEntry . Version . ToString ( ) ) ;
4849 var currentVersion = Version . Parse ( Constant . Version ) ;
@@ -58,7 +59,7 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
5859
5960 if ( ! silentUpdate )
6061 api . ShowMsg ( api . GetTranslation ( "update_flowlauncher_update_found" ) ,
61- api . GetTranslation ( "update_flowlauncher_updating" ) ) ;
62+ api . GetTranslation ( "update_flowlauncher_updating" ) ) ;
6263
6364 await updateManager . DownloadReleases ( newUpdateInfo . ReleasesToApply ) . ConfigureAwait ( false ) ;
6465
@@ -70,8 +71,8 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
7071 FilesFolders . CopyAll ( DataLocation . PortableDataPath , targetDestination ) ;
7172 if ( ! FilesFolders . VerifyBothFolderFilesEqual ( DataLocation . PortableDataPath , targetDestination ) )
7273 MessageBox . Show ( string . Format ( api . GetTranslation ( "update_flowlauncher_fail_moving_portable_user_profile_data" ) ,
73- DataLocation . PortableDataPath ,
74- targetDestination ) ) ;
74+ DataLocation . PortableDataPath ,
75+ targetDestination ) ) ;
7576 }
7677 else
7778 {
@@ -87,12 +88,15 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
8788 UpdateManager . RestartApp ( Constant . ApplicationFileName ) ;
8889 }
8990 }
90- catch ( Exception e ) when ( e is HttpRequestException || e is WebException || e is SocketException || e . InnerException is TimeoutException )
91+ catch ( Exception e ) when ( e is HttpRequestException or WebException or SocketException || e . InnerException is TimeoutException )
9192 {
9293 Log . Exception ( $ "|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e ) ;
9394 api . ShowMsg ( api . GetTranslation ( "update_flowlauncher_fail" ) ,
94- api . GetTranslation ( "update_flowlauncher_check_connection" ) ) ;
95- return ;
95+ api . GetTranslation ( "update_flowlauncher_check_connection" ) ) ;
96+ }
97+ finally
98+ {
99+ UpdateLock . Release ( ) ;
96100 }
97101 }
98102
@@ -115,13 +119,16 @@ private async Task<UpdateManager> GitHubUpdateManager(string repository)
115119 var uri = new Uri ( repository ) ;
116120 var api = $ "https://api.github.com/repos{ uri . AbsolutePath } /releases";
117121
118- var jsonStream = await Http . GetStreamAsync ( api ) . ConfigureAwait ( false ) ;
122+ await using var jsonStream = await Http . GetStreamAsync ( api ) . ConfigureAwait ( false ) ;
119123
120124 var releases = await System . Text . Json . JsonSerializer . DeserializeAsync < List < GithubRelease > > ( jsonStream ) . ConfigureAwait ( false ) ;
121125 var latest = releases . Where ( r => ! r . Prerelease ) . OrderByDescending ( r => r . PublishedAt ) . First ( ) ;
122126 var latestUrl = latest . HtmlUrl . Replace ( "/tag/" , "/download/" ) ;
123-
124- var client = new WebClient { Proxy = Http . WebProxy } ;
127+
128+ var client = new WebClient
129+ {
130+ Proxy = Http . WebProxy
131+ } ;
125132 var downloader = new FileDownloader ( client ) ;
126133
127134 var manager = new UpdateManager ( latestUrl , urlDownloader : downloader ) ;
0 commit comments