hermes-client/Chat/Observers/TTSPublisher.cs

33 lines
910 B
C#
Raw Normal View History

using TwitchChatTTS.Chat.Speech;
namespace TwitchChatTTS.Chat.Observers
{
public class TTSPublisher : IObservable<TTSGroupedMessage>
{
private readonly HashSet<IObserver<TTSGroupedMessage>> _observers;
public TTSPublisher()
{
_observers = new();
}
public IDisposable Subscribe(IObserver<TTSGroupedMessage> observer)
{
2024-11-08 10:26:10 -05:00
_observers.Add(observer);
return new Unsubscriber<TTSGroupedMessage>(_observers, observer);
}
}
internal sealed class Unsubscriber<T> : IDisposable
{
private readonly ISet<IObserver<T>> _observers;
private readonly IObserver<T> _observer;
internal Unsubscriber(ISet<IObserver<T>> observers, IObserver<T> observer)
=> (_observers, _observer) = (observers, observer);
public void Dispose() => _observers.Remove(_observer);
}
}