Migrated the update check function from Tauri JS to the runtime API

This commit is contained in:
Thorsten Sommer 2024-08-28 08:57:40 +02:00
parent 1d12facd58
commit a41a86165f
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 57 additions and 53 deletions

View File

@ -85,8 +85,8 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, IDis
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.UPDATE_AVAILABLE, Event.USER_SEARCH_FOR_UPDATE, Event.CONFIGURATION_CHANGED ]);
// Set the js runtime for the update service:
UpdateService.SetBlazorDependencies(this.JsRuntime, this.Snackbar);
// Set the snackbar for the update service:
UpdateService.SetBlazorDependencies(this.Snackbar);
TemporaryChatService.Initialize();
// Should the navigation bar be open by default?

View File

@ -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));
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)

View File

@ -7,10 +7,6 @@ namespace AIStudio.Tools.Services;
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 ISnackbar? SNACKBAR;
@ -96,7 +92,7 @@ public sealed class UpdateService : BackgroundService, IMessageBusReceiver
if(!IS_INITIALIZED)
return;
var response = await this.rust.CheckForUpdate(JS_RUNTIME!);
var response = await this.rust.CheckForUpdate();
if (response.UpdateIsAvailable)
{
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;
JS_RUNTIME = jsRuntime;
IS_INITIALIZED = true;
}
}

View File

@ -194,7 +194,7 @@ async fn main() {
//
tauri::async_runtime::spawn(async move {
_ = 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()
.launch().await.unwrap();
});
@ -312,8 +312,7 @@ async fn main() {
})
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![
store_secret, get_secret, delete_secret,
check_for_update, install_update
store_secret, get_secret, delete_secret, install_update
])
.build(tauri::generate_context!())
.expect("Error while running Tauri application");
@ -673,10 +672,9 @@ fn stop_servers() {
}
}
#[tauri::command]
async fn check_for_update() -> CheckUpdateResponse {
#[get("/updates/check")]
async fn check_for_update() -> Json<CheckUpdateResponse> {
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;
match response {
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();
info!(Source = "Updater"; "An update to version '{new_version}' is available.");
let changelog = update_response.body();
CheckUpdateResponse {
Json(CheckUpdateResponse {
update_is_available: true,
error: false,
new_version: new_version.to_string(),
@ -693,31 +691,30 @@ async fn check_for_update() -> CheckUpdateResponse {
Some(c) => c.to_string(),
None => String::from(""),
},
}
})
},
false => {
info!(Source = "Updater"; "No updates are available.");
CheckUpdateResponse {
Json(CheckUpdateResponse {
update_is_available: false,
error: false,
new_version: String::from(""),
changelog: String::from(""),
}
})
},
},
Err(e) => {
warn!(Source = "Updater"; "Failed to check for updates: {e}.");
CheckUpdateResponse {
Json(CheckUpdateResponse {
update_is_available: false,
error: true,
new_version: String::from(""),
changelog: String::from(""),
}
})
},
}
}).await.unwrap()
}
#[derive(Serialize)]