Switch to message buffer / dispatcher for Twitch event sub messages

This commit is contained in:
WarmUpTill
2024-01-30 23:20:40 +01:00
committed by WarmUpTill
parent 41033f2230
commit a62883153c
4 changed files with 77 additions and 76 deletions

View File

@@ -3,7 +3,6 @@
#include "twitch-helpers.hpp"
#include <log-helper.hpp>
#include <plugin-state-helpers.hpp>
namespace advss {
@@ -27,6 +26,8 @@ static constexpr std::string_view registerSubscriptionPath =
#endif
static const int reconnectDelay = 15;
#undef DispatchMessage
EventSub::EventSub() : QObject(nullptr)
{
_client.get_alog().clear_channels(
@@ -59,20 +60,6 @@ EventSub::~EventSub()
UnregisterInstance();
}
static bool setupEventSubMessageClear()
{
AddIntervalResetStep(&EventSub::ClearAllEvents);
return true;
}
bool EventSub::_setupDone = setupEventSubMessageClear();
void EventSub::ClearEvents()
{
std::lock_guard<std::mutex> lock(_messageMtx);
_messages.clear();
}
std::mutex EventSub::_instancesMtx;
std::vector<EventSub *> EventSub::_instances;
@@ -89,14 +76,6 @@ void EventSub::UnregisterInstance()
_instances.erase(it, _instances.end());
}
void EventSub::ClearAllEvents()
{
std::lock_guard<std::mutex> lock(_instancesMtx);
for (const auto &eventSub : _instances) {
eventSub->ClearEvents();
}
}
void EventSub::ConnectThread()
{
while (!_disconnect) {
@@ -169,10 +148,9 @@ void EventSub::Disconnect()
ClearActiveSubscriptions();
}
std::vector<Event> EventSub::Events()
EventSubMessageBuffer EventSub::RegisterForEvents()
{
std::lock_guard<std::mutex> lock(_messageMtx);
return _messages;
return _dispatcher.RegisterClient();
}
bool EventSub::SubscriptionIsActive(const std::string &id)
@@ -358,8 +336,7 @@ void EventSub::HandleNotification(obs_data_t *data)
event.type = obs_data_get_string(subscription, "type");
OBSDataAutoRelease eventData = obs_data_get_obj(data, "event");
event.data = eventData;
std::lock_guard<std::mutex> lock(_messageMtx);
_messages.emplace_back(event);
_dispatcher.DispatchMessage(event);
}
void EventSub::HandleReconnect(obs_data_t *data)