mirror of
				https://github.com/MindWorkAI/AI-Studio.git
				synced 2025-10-31 22:20:20 +00:00 
			
		
		
		
	Migrated the update check function from Tauri JS to the runtime API
This commit is contained in:
		
							parent
							
								
									1d12facd58
								
							
						
					
					
						commit
						a41a86165f
					
				| @ -85,8 +85,8 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, IDis | |||||||
|         this.MessageBus.RegisterComponent(this); |         this.MessageBus.RegisterComponent(this); | ||||||
|         this.MessageBus.ApplyFilters(this, [], [ Event.UPDATE_AVAILABLE, Event.USER_SEARCH_FOR_UPDATE, Event.CONFIGURATION_CHANGED ]); |         this.MessageBus.ApplyFilters(this, [], [ Event.UPDATE_AVAILABLE, Event.USER_SEARCH_FOR_UPDATE, Event.CONFIGURATION_CHANGED ]); | ||||||
|          |          | ||||||
|         // Set the js runtime for the update service: |         // Set the snackbar for the update service: | ||||||
|         UpdateService.SetBlazorDependencies(this.JsRuntime, this.Snackbar); |         UpdateService.SetBlazorDependencies(this.Snackbar); | ||||||
|         TemporaryChatService.Initialize(); |         TemporaryChatService.Initialize(); | ||||||
|          |          | ||||||
|         // Should the navigation bar be open by default? |         // Should the navigation bar be open by default? | ||||||
|  | |||||||
| @ -118,10 +118,22 @@ public sealed class Rust(string apiPort) : IDisposable | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public async Task<UpdateResponse> CheckForUpdate(IJSRuntime jsRuntime) |     public async Task<UpdateResponse> CheckForUpdate() | ||||||
|  |     { | ||||||
|  |         try | ||||||
|         { |         { | ||||||
|             var cts = new CancellationTokenSource(TimeSpan.FromSeconds(16)); |             var cts = new CancellationTokenSource(TimeSpan.FromSeconds(16)); | ||||||
|         return await jsRuntime.InvokeAsync<UpdateResponse>("window.__TAURI__.invoke", cts.Token, "check_for_update"); |             return await this.http.GetFromJsonAsync<UpdateResponse>("/updates/check", cts.Token); | ||||||
|  |         } | ||||||
|  |         catch (Exception e) | ||||||
|  |         { | ||||||
|  |             this.logger!.LogError(e, "Failed to check for an update."); | ||||||
|  |             return new UpdateResponse | ||||||
|  |             { | ||||||
|  |                 Error = true, | ||||||
|  |                 UpdateIsAvailable = false, | ||||||
|  |             }; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public async Task InstallUpdate(IJSRuntime jsRuntime) |     public async Task InstallUpdate(IJSRuntime jsRuntime) | ||||||
|  | |||||||
| @ -7,10 +7,6 @@ namespace AIStudio.Tools.Services; | |||||||
| 
 | 
 | ||||||
| public sealed class UpdateService : BackgroundService, IMessageBusReceiver | public sealed class UpdateService : BackgroundService, IMessageBusReceiver | ||||||
| { | { | ||||||
|     // We cannot inject IJSRuntime into our service. This is because |  | ||||||
|     // the service is not a Blazor component. We need to pass the IJSRuntime from |  | ||||||
|     // the MainLayout component to the service. |  | ||||||
|     private static IJSRuntime? JS_RUNTIME; |  | ||||||
|     private static bool IS_INITIALIZED; |     private static bool IS_INITIALIZED; | ||||||
|     private static ISnackbar? SNACKBAR; |     private static ISnackbar? SNACKBAR; | ||||||
|      |      | ||||||
| @ -96,7 +92,7 @@ public sealed class UpdateService : BackgroundService, IMessageBusReceiver | |||||||
|         if(!IS_INITIALIZED) |         if(!IS_INITIALIZED) | ||||||
|             return; |             return; | ||||||
|          |          | ||||||
|         var response = await this.rust.CheckForUpdate(JS_RUNTIME!); |         var response = await this.rust.CheckForUpdate(); | ||||||
|         if (response.UpdateIsAvailable) |         if (response.UpdateIsAvailable) | ||||||
|         { |         { | ||||||
|             await this.messageBus.SendMessage(null, Event.UPDATE_AVAILABLE, response); |             await this.messageBus.SendMessage(null, Event.UPDATE_AVAILABLE, response); | ||||||
| @ -115,10 +111,9 @@ public sealed class UpdateService : BackgroundService, IMessageBusReceiver | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public static void SetBlazorDependencies(IJSRuntime jsRuntime, ISnackbar snackbar) |     public static void SetBlazorDependencies(ISnackbar snackbar) | ||||||
|     { |     { | ||||||
|         SNACKBAR = snackbar; |         SNACKBAR = snackbar; | ||||||
|         JS_RUNTIME = jsRuntime; |  | ||||||
|         IS_INITIALIZED = true; |         IS_INITIALIZED = true; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -194,7 +194,7 @@ async fn main() { | |||||||
|     //
 |     //
 | ||||||
|     tauri::async_runtime::spawn(async move { |     tauri::async_runtime::spawn(async move { | ||||||
|         _ = rocket::custom(figment) |         _ = rocket::custom(figment) | ||||||
|             .mount("/", routes![dotnet_port, dotnet_ready, set_clipboard]) |             .mount("/", routes![dotnet_port, dotnet_ready, set_clipboard, check_for_update]) | ||||||
|             .ignite().await.unwrap() |             .ignite().await.unwrap() | ||||||
|             .launch().await.unwrap(); |             .launch().await.unwrap(); | ||||||
|     }); |     }); | ||||||
| @ -312,8 +312,7 @@ async fn main() { | |||||||
|         }) |         }) | ||||||
|         .plugin(tauri_plugin_window_state::Builder::default().build()) |         .plugin(tauri_plugin_window_state::Builder::default().build()) | ||||||
|         .invoke_handler(tauri::generate_handler![ |         .invoke_handler(tauri::generate_handler![ | ||||||
|             store_secret, get_secret, delete_secret, |             store_secret, get_secret, delete_secret, install_update | ||||||
|             check_for_update, install_update |  | ||||||
|         ]) |         ]) | ||||||
|         .build(tauri::generate_context!()) |         .build(tauri::generate_context!()) | ||||||
|         .expect("Error while running Tauri application"); |         .expect("Error while running Tauri application"); | ||||||
| @ -673,10 +672,9 @@ fn stop_servers() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[tauri::command] | #[get("/updates/check")] | ||||||
| async fn check_for_update() -> CheckUpdateResponse { | async fn check_for_update() -> Json<CheckUpdateResponse> { | ||||||
|     let app_handle = MAIN_WINDOW.lock().unwrap().as_ref().unwrap().app_handle(); |     let app_handle = MAIN_WINDOW.lock().unwrap().as_ref().unwrap().app_handle(); | ||||||
|     tauri::async_runtime::spawn(async move { |  | ||||||
|     let response = app_handle.updater().check().await; |     let response = app_handle.updater().check().await; | ||||||
|     match response { |     match response { | ||||||
|         Ok(update_response) => match update_response.is_update_available() { |         Ok(update_response) => match update_response.is_update_available() { | ||||||
| @ -685,7 +683,7 @@ async fn check_for_update() -> CheckUpdateResponse { | |||||||
|                 let new_version = update_response.latest_version(); |                 let new_version = update_response.latest_version(); | ||||||
|                 info!(Source = "Updater"; "An update to version '{new_version}' is available."); |                 info!(Source = "Updater"; "An update to version '{new_version}' is available."); | ||||||
|                 let changelog = update_response.body(); |                 let changelog = update_response.body(); | ||||||
|                     CheckUpdateResponse { |                 Json(CheckUpdateResponse { | ||||||
|                     update_is_available: true, |                     update_is_available: true, | ||||||
|                     error: false, |                     error: false, | ||||||
|                     new_version: new_version.to_string(), |                     new_version: new_version.to_string(), | ||||||
| @ -693,31 +691,30 @@ async fn check_for_update() -> CheckUpdateResponse { | |||||||
|                         Some(c) => c.to_string(), |                         Some(c) => c.to_string(), | ||||||
|                         None => String::from(""), |                         None => String::from(""), | ||||||
|                     }, |                     }, | ||||||
|                     } |                 }) | ||||||
|             }, |             }, | ||||||
| 
 | 
 | ||||||
|             false => { |             false => { | ||||||
|                 info!(Source = "Updater"; "No updates are available."); |                 info!(Source = "Updater"; "No updates are available."); | ||||||
|                     CheckUpdateResponse { |                 Json(CheckUpdateResponse { | ||||||
|                     update_is_available: false, |                     update_is_available: false, | ||||||
|                     error: false, |                     error: false, | ||||||
|                     new_version: String::from(""), |                     new_version: String::from(""), | ||||||
|                     changelog: String::from(""), |                     changelog: String::from(""), | ||||||
|                     } |                 }) | ||||||
|             }, |             }, | ||||||
|         }, |         }, | ||||||
| 
 | 
 | ||||||
|         Err(e) => { |         Err(e) => { | ||||||
|             warn!(Source = "Updater"; "Failed to check for updates: {e}."); |             warn!(Source = "Updater"; "Failed to check for updates: {e}."); | ||||||
|                 CheckUpdateResponse { |             Json(CheckUpdateResponse { | ||||||
|                 update_is_available: false, |                 update_is_available: false, | ||||||
|                 error: true, |                 error: true, | ||||||
|                 new_version: String::from(""), |                 new_version: String::from(""), | ||||||
|                 changelog: String::from(""), |                 changelog: String::from(""), | ||||||
|                 } |             }) | ||||||
|         }, |         }, | ||||||
|     } |     } | ||||||
|     }).await.unwrap() |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Serialize)] | #[derive(Serialize)] | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user