Chatterino
PubSubHelpers.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <QJsonObject>
4 #include <boost/asio.hpp>
5 #include <boost/asio/steady_timer.hpp>
6 #include <memory>
7 #include "common/QLogging.hpp"
9 
10 namespace chatterino {
11 
12 class TwitchAccount;
13 struct ActionUser;
14 
15 // Create timer using given ioService
16 template <typename Duration, typename Callback>
17 void runAfter(boost::asio::io_service &ioService, Duration duration,
18  Callback cb)
19 {
20  auto timer = std::make_shared<boost::asio::steady_timer>(ioService);
21  timer->expires_from_now(duration);
22 
23  timer->async_wait([timer, cb](const boost::system::error_code &ec) {
24  if (ec)
25  {
26  qCDebug(chatterinoPubSub)
27  << "Error in runAfter:" << ec.message().c_str();
28  return;
29  }
30 
31  cb(timer);
32  });
33 }
34 
35 // Use provided timer
36 template <typename Duration, typename Callback>
37 void runAfter(std::shared_ptr<boost::asio::steady_timer> timer,
38  Duration duration, Callback cb)
39 {
40  timer->expires_from_now(duration);
41 
42  timer->async_wait([timer, cb](const boost::system::error_code &ec) {
43  if (ec)
44  {
45  qCDebug(chatterinoPubSub)
46  << "Error in runAfter:" << ec.message().c_str();
47  return;
48  }
49 
50  cb(timer);
51  });
52 }
53 
54 } // namespace chatterino
Definition: Application.cpp:48
void runAfter(boost::asio::io_service &ioService, Duration duration, Callback cb)
Definition: PubSubHelpers.hpp:17