BotSharp/src/Infrastructure/BotSharp.Core/Observables/Queues/MessageHub.cs

44 lines
1 KiB
C#
Raw Normal View History

2025-06-17 18:17:28 +00:00
using System.Reactive.Subjects;
namespace BotSharp.Core.Observables.Queues;
2025-06-27 18:49:44 +00:00
public class MessageHub<T> where T : class
2025-06-17 18:17:28 +00:00
{
2025-06-27 18:49:44 +00:00
private readonly ILogger<MessageHub<T>> _logger;
private readonly ISubject<T> _observable = new Subject<T>();
public IObservable<T> Events => _observable;
2025-06-17 18:17:28 +00:00
2025-06-27 18:49:44 +00:00
public MessageHub(ILogger<MessageHub<T>> logger)
2025-06-17 18:17:28 +00:00
{
_logger = logger;
}
/// <summary>
/// Push an item to the observers.
/// </summary>
/// <param name="item"></param>
2025-06-27 18:49:44 +00:00
public void Push(T item)
2025-06-17 18:17:28 +00:00
{
_observable.OnNext(item);
}
/// <summary>
/// Send a complete notification to the observers.
/// This will stop the observers from receiving data.
/// </summary>
public void Complete()
{
_observable.OnCompleted();
}
/// <summary>
/// Send an error notification to the observers.
/// This will stop the observers from receiving data.
/// </summary>
/// <param name="error"></param>
public void Error(Exception error)
{
_observable.OnError(error);
}
}