using Immense.SimpleMessenger; using Microsoft.AspNetCore.Components; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Remotely.Server.Components; public class MessengerSubscriber : ComponentBase, IDisposable { protected readonly ConcurrentQueue _registrations = new(); [Inject] protected IMessenger Messenger { get; init; } = null!; public void Dispose() { while (_registrations.TryDequeue(out var registration)) { try { registration.Dispose(); } catch { } } GC.SuppressFinalize(this); } protected Task Register(TChannel channel, RegistrationCallback handler) where TMessage : class where TChannel : IEquatable { var registration = Messenger.Register(this, channel, handler); _registrations.Enqueue(registration); return Task.CompletedTask; } protected Task Register(RegistrationCallback handler) where TMessage : class { var registration = Messenger.Register(this, handler); _registrations.Enqueue(registration); return Task.CompletedTask; } }