Implemented the .NET side of Tauri event stream handling

This commit is contained in:
Thorsten Sommer 2025-10-21 19:18:54 +02:00
parent 398fd613ec
commit 77bd3b389c
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
6 changed files with 83 additions and 3 deletions

View File

@ -133,6 +133,11 @@ internal sealed class Program
builder.Services.AddHostedService<UpdateService>(); builder.Services.AddHostedService<UpdateService>();
builder.Services.AddHostedService<TemporaryChatService>(); builder.Services.AddHostedService<TemporaryChatService>();
builder.Services.AddHostedService<EnterpriseEnvironmentService>(); builder.Services.AddHostedService<EnterpriseEnvironmentService>();
// ReSharper disable AccessToDisposedClosure
builder.Services.AddHostedService<RustService>(_ => rust);
// ReSharper restore AccessToDisposedClosure
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents() .AddInteractiveServerComponents()
.AddHubOptions(options => .AddHubOptions(options =>

View File

@ -14,6 +14,7 @@ public enum Event
SHOW_ERROR, SHOW_ERROR,
SHOW_WARNING, SHOW_WARNING,
SHOW_SUCCESS, SHOW_SUCCESS,
TAURI_EVENT_RECEIVED,
// Update events: // Update events:
USER_SEARCH_FOR_UPDATE, USER_SEARCH_FOR_UPDATE,

View File

@ -0,0 +1,3 @@
namespace AIStudio.Tools.Rust;
public readonly record struct TauriEvent(TauriEventType Type, List<string> Payload);

View File

@ -0,0 +1,11 @@
namespace AIStudio.Tools.Rust;
public enum TauriEventType
{
WINDOW_FOCUSED,
WINDOW_NOT_FOCUSED,
FILE_DROP_HOVERED,
FILE_DROP_DROPPED,
FILE_DROP_CANCELED,
}

View File

@ -0,0 +1,53 @@
using System.Text.Json;
using AIStudio.Tools.Rust;
namespace AIStudio.Tools.Services;
public partial class RustService
{
private async Task StartStreamTauriEvents(CancellationToken stopToken)
{
try
{
while (!stopToken.IsCancellationRequested)
{
try
{
await using var stream = await this.http.GetStreamAsync("/events", stopToken);
using var reader = new StreamReader(stream);
while(!reader.EndOfStream)
{
var line = await reader.ReadLineAsync(stopToken);
if (string.IsNullOrWhiteSpace(line))
continue;
var tauriEvent = JsonSerializer.Deserialize<TauriEvent>(line, this.jsonRustSerializerOptions);
if (tauriEvent != default)
await MessageBus.INSTANCE.SendMessage(null, Event.TAURI_EVENT_RECEIVED, tauriEvent);
}
}
// The cancellation token was triggered, exit the loop:
catch (OperationCanceledException)
{
break;
}
// Some other error occurred, log it and retry after a delay:
catch (Exception e)
{
this.logger!.LogError("Error while streaming Tauri events: {Message}", e.Message);
await Task.Delay(TimeSpan.FromSeconds(3), stopToken);
}
}
}
// The cancellation token was triggered, exit the method:
catch (OperationCanceledException)
{
}
this.logger!.LogWarning("Stopped streaming Tauri events.");
}
}

View File

@ -8,7 +8,7 @@ namespace AIStudio.Tools.Services;
/// <summary> /// <summary>
/// Calling Rust functions. /// Calling Rust functions.
/// </summary> /// </summary>
public sealed partial class RustService : IDisposable public sealed partial class RustService : BackgroundService
{ {
private readonly HttpClient http; private readonly HttpClient http;
@ -59,11 +59,18 @@ public sealed partial class RustService : IDisposable
this.encryptor = encryptionService; this.encryptor = encryptionService;
} }
#region IDisposable #region Overrides of BackgroundService
public void Dispose() protected override async Task ExecuteAsync(CancellationToken stopToken)
{
this.logger?.LogInformation("The Rust service was initialized.");
await this.StartStreamTauriEvents(stopToken);
}
public override void Dispose()
{ {
this.http.Dispose(); this.http.Dispose();
base.Dispose();
} }
#endregion #endregion