using TwitchChatTTS.Chat.Speech; namespace TwitchChatTTS.Chat.Observers { public class TTSPublisher : IObservable { private readonly HashSet> _observers; private readonly HashSet _messages; public TTSPublisher() { _observers = new(); _messages = new(); } public IDisposable Subscribe(IObserver observer) { if (_observers.Add(observer)) { foreach (var item in _messages) observer.OnNext(item); } return new Unsubscriber(_observers, observer); } } internal sealed class Unsubscriber : IDisposable { private readonly ISet> _observers; private readonly IObserver _observer; internal Unsubscriber(ISet> observers, IObserver observer) => (_observers, _observer) = (observers, observer); public void Dispose() => _observers.Remove(_observer); } }