Skip to content

Commit

Permalink
refactor: Twitch PubSub client (#5059)
Browse files Browse the repository at this point in the history
* Remove unused `setAccountData` function

* Move PubSub out of TwitchIrcServer and into Application

* Add changelog entry

* fix: assert feedback

* Add PubSub::unlistenPrefix as per review suggestion

* Fix tests

* quit pubsub on exit

might conflict with exit removal, so can be reverted but this shows it's possible

* Don't manually call stop on clients, it's called when the connection is closed

* nit: rename `mainThread` to `thread`

* Join in a thread!!!!!!!!
  • Loading branch information
pajlada authored Jan 6, 2024
1 parent e48d868 commit 416806b
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 458 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
- Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921)
- Dev: Refactor `common/Credentials`. (#4979)
- Dev: Refactor chat logger. (#5058)
- Dev: Refactor Twitch PubSub client. (#5059)
- Dev: Changed lifetime of context menus. (#4924)
- Dev: Renamed `tools` directory to `scripts`. (#5035)
- Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926)
Expand Down
6 changes: 6 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class EmptyApplication : public IApplication
return nullptr;
}

PubSub *getTwitchPubSub() override
{
assert(false && "getTwitchPubSub was called without being initialized");
return nullptr;
}

Logging *getChatLogger() override
{
assert(!"getChatLogger was called without being initialized");
Expand Down
434 changes: 213 additions & 221 deletions src/Application.cpp

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class IApplication
virtual HighlightController *getHighlights() = 0;
virtual NotificationController *getNotifications() = 0;
virtual ITwitchIrcServer *getTwitch() = 0;
virtual PubSub *getTwitchPubSub() = 0;
virtual Logging *getChatLogger() = 0;
virtual ChatterinoBadges *getChatterinoBadges() = 0;
virtual FfzBadges *getFfzBadges() = 0;
Expand Down Expand Up @@ -97,6 +98,12 @@ class Application : public IApplication
Application &operator=(const Application &) = delete;
Application &operator=(Application &&) = delete;

/**
* In the interim, before we remove _exit(0); from RunGui.cpp,
* this will destroy things we know can be destroyed
*/
void fakeDtor();

void initialize(Settings &settings, Paths &paths);
void load();
void save();
Expand Down Expand Up @@ -128,6 +135,7 @@ class Application : public IApplication

private:
TwitchLiveController *const twitchLiveController{};
std::unique_ptr<PubSub> twitchPubSub;
const std::unique_ptr<Logging> logging;

public:
Expand Down Expand Up @@ -181,6 +189,7 @@ class Application : public IApplication
return this->highlights;
}
ITwitchIrcServer *getTwitch() override;
PubSub *getTwitchPubSub() override;
Logging *getChatLogger() override;
ChatterinoBadges *getChatterinoBadges() override
{
Expand Down
2 changes: 2 additions & 0 deletions src/RunGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ void runGui(QApplication &a, Paths &paths, Settings &settings, const Args &args)
flushClipboard();
#endif

app.fakeDtor();

_exit(0);
}
} // namespace chatterino
7 changes: 5 additions & 2 deletions src/providers/twitch/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ PubSubClient::PubSubClient(WebsocketClient &websocketClient,
const PubSubClientOptions &clientOptions)
: websocketClient_(websocketClient)
, handle_(handle)
, heartbeatTimer_(std::make_shared<boost::asio::steady_timer>(
this->websocketClient_.get_io_service()))
, clientOptions_(clientOptions)
{
}
Expand All @@ -40,6 +42,7 @@ void PubSubClient::stop()
assert(this->started_);

this->started_ = false;
this->heartbeatTimer_->cancel();
}

void PubSubClient::close(const std::string &reason,
Expand Down Expand Up @@ -187,8 +190,8 @@ void PubSubClient::ping()

auto self = this->shared_from_this();

runAfter(this->websocketClient_.get_io_service(),
this->clientOptions_.pingInterval_, [self](auto timer) {
runAfter(this->heartbeatTimer_, this->clientOptions_.pingInterval_,
[self](auto timer) {
if (!self->started_)
{
return;
Expand Down
1 change: 1 addition & 0 deletions src/providers/twitch/PubSubClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class PubSubClient : public std::enable_shared_from_this<PubSubClient>
std::atomic<bool> awaitingPong_{false};
std::atomic<bool> started_{false};

std::shared_ptr<boost::asio::steady_timer> heartbeatTimer_;
const PubSubClientOptions &clientOptions_;
};

Expand Down
Loading

0 comments on commit 416806b

Please sign in to comment.