using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Disposables; using Avalonia.Threading; using Microsoft.Extensions.Logging; using Wabbajack.App.Messages; namespace Wabbajack.App; public interface IMessageBus { public void Send(T message); } public class MessageBus : IMessageBus { private readonly ILogger _logger; private readonly IReceiverMarker[] _receivers; public MessageBus(ILogger logger, IEnumerable receivers) { Instance = this; _receivers = receivers.ToArray(); _logger = logger; } public static IMessageBus Instance { get; set; } public void Send(T message) { AvaloniaScheduler.Instance.Schedule(message, TimeSpan.FromMilliseconds(200), (_, msg) => { foreach (var receiver in _receivers.OfType>()) { _logger.LogInformation("Sending {msg} to {receiver}", msg, receiver); try { receiver.Receive(msg); } catch (Exception ex) { _logger.LogCritical(ex, "Failed sending {msg} to {receiver}", msg, receiver); } } return Disposable.Empty; }); } }