Added a simple method for subscribing to events.

This commit is contained in:
Tom 2025-01-07 15:42:10 +00:00
parent 64cb0c1f6d
commit b724cd00eb
4 changed files with 15 additions and 8 deletions

View File

@ -37,7 +37,7 @@ namespace TwitchChatTTS.Bus
{ {
if (!_topics.TryGetValue(topic, out var bus)) if (!_topics.TryGetValue(topic, out var bus))
{ {
bus = new ServiceBusObservable(topic, this); bus = new ServiceBusObservable(topic, this, _logger);
_topics.Add(topic, bus); _topics.Add(topic, bus);
} }
return bus; return bus;

View File

@ -1,4 +1,5 @@
using System.Reactive; using System.Reactive;
using Serilog;
namespace TwitchChatTTS.Bus namespace TwitchChatTTS.Bus
{ {
@ -6,11 +7,13 @@ namespace TwitchChatTTS.Bus
{ {
private readonly string _topic; private readonly string _topic;
private readonly ServiceBusCentral _central; private readonly ServiceBusCentral _central;
private readonly ILogger _logger;
public ServiceBusObservable(string topic, ServiceBusCentral central) public ServiceBusObservable(string topic, ServiceBusCentral central, ILogger logger)
{ {
_topic = topic; _topic = topic;
_central = central; _central = central;
_logger = logger;
} }
protected override IDisposable SubscribeCore(IObserver<ServiceBusData> observer) protected override IDisposable SubscribeCore(IObserver<ServiceBusData> observer)
@ -19,6 +22,10 @@ namespace TwitchChatTTS.Bus
return new ServiceBusUnsubscriber(_topic, _central, observer); return new ServiceBusUnsubscriber(_topic, _central, observer);
} }
public IDisposable Subscribe(Action<ServiceBusData> action) {
return Subscribe(new ServiceBusObserver(action, _logger));
}
private sealed class ServiceBusUnsubscriber : IDisposable private sealed class ServiceBusUnsubscriber : IDisposable
{ {
private readonly string _topic; private readonly string _topic;

View File

@ -66,18 +66,18 @@ namespace TwitchChatTTS.Hermes.Socket
_lock = new object(); _lock = new object();
var ttsCreateUserVoice = _bus.GetTopic("tts.user.voice.create"); var ttsCreateUserVoice = _bus.GetTopic("tts.user.voice.create");
ttsCreateUserVoice.Subscribe(new ServiceBusObserver(async data => await Send(3, new RequestMessage() ttsCreateUserVoice.Subscribe(async data => await Send(3, new RequestMessage()
{ {
Type = "create_tts_user", Type = "create_tts_user",
Data = (IDictionary<string, object>) data.Value! Data = (IDictionary<string, object>) data.Value!
}), logger)); }));
var ttsUpdateUserVoice = _bus.GetTopic("tts.user.voice.update"); var ttsUpdateUserVoice = _bus.GetTopic("tts.user.voice.update");
ttsUpdateUserVoice.Subscribe(new ServiceBusObserver(async data => await Send(3, new RequestMessage() ttsUpdateUserVoice.Subscribe(async data => await Send(3, new RequestMessage()
{ {
Type = "update_tts_user", Type = "update_tts_user",
Data = (IDictionary<string, object>) data.Value! Data = (IDictionary<string, object>) data.Value!
}), logger)); }));
} }

View File

@ -53,7 +53,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
_lock = new object(); _lock = new object();
var topic = _bus.GetTopic("redemptions_initiation"); var topic = _bus.GetTopic("redemptions_initiation");
topic.Subscribe(new ServiceBusObserver(data => topic.Subscribe(data =>
{ {
if (data.Value is not RedemptionInitiation init) if (data.Value is not RedemptionInitiation init)
return; return;
@ -76,7 +76,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
Add(redemption); Add(redemption);
Initialize(); Initialize();
}, _logger)); });
} }
public void Add(RedeemableAction action) public void Add(RedeemableAction action)