mirror-chatterino2/src/providers/twitch/PubsubHelpers.hpp

67 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <memory>
#include "common/QLogging.hpp"
2018-08-15 22:46:20 +02:00
#include "util/RapidjsonHelpers.hpp"
namespace chatterino {
class TwitchAccount;
struct ActionUser;
const rapidjson::Value &getArgs(const rapidjson::Value &data);
const rapidjson::Value &getMsgID(const rapidjson::Value &data);
bool getCreatedByUser(const rapidjson::Value &data, ActionUser &user);
bool getTargetUser(const rapidjson::Value &data, ActionUser &user);
bool getTargetUserName(const rapidjson::Value &data, ActionUser &user);
rapidjson::Document createListenMessage(const std::vector<QString> &topicsVec,
std::shared_ptr<TwitchAccount> account);
2018-08-06 21:17:03 +02:00
rapidjson::Document createUnlistenMessage(
const std::vector<QString> &topicsVec);
// Create timer using given ioService
template <typename Duration, typename Callback>
2018-08-06 21:17:03 +02:00
void runAfter(boost::asio::io_service &ioService, Duration duration,
Callback cb)
{
auto timer = std::make_shared<boost::asio::steady_timer>(ioService);
timer->expires_from_now(duration);
timer->async_wait([timer, cb](const boost::system::error_code &ec) {
2018-10-21 13:43:02 +02:00
if (ec)
{
qCDebug(chatterinoPubsub)
<< "Error in runAfter:" << ec.message().c_str();
return;
}
cb(timer);
});
}
// Use provided timer
template <typename Duration, typename Callback>
2018-08-06 21:17:03 +02:00
void runAfter(std::shared_ptr<boost::asio::steady_timer> timer,
Duration duration, Callback cb)
{
timer->expires_from_now(duration);
timer->async_wait([timer, cb](const boost::system::error_code &ec) {
2018-10-21 13:43:02 +02:00
if (ec)
{
qCDebug(chatterinoPubsub)
<< "Error in runAfter:" << ec.message().c_str();
return;
}
cb(timer);
});
}
} // namespace chatterino