Fix timer stuff (#580)

* Add and sort Network files to chatterino project file
This commit is contained in:
pajlada 2018-07-07 15:50:05 +02:00 committed by GitHub
parent 55269587f5
commit 2ea3643100
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 45 deletions

View file

@ -102,10 +102,11 @@ SOURCES += \
src/common/Channel.cpp \ src/common/Channel.cpp \
src/common/CompletionModel.cpp \ src/common/CompletionModel.cpp \
src/common/Emotemap.cpp \ src/common/Emotemap.cpp \
src/common/NetworkManager.cpp \
src/common/NetworkResult.cpp \
src/common/NetworkData.cpp \ src/common/NetworkData.cpp \
src/common/NetworkManager.cpp \
src/common/NetworkRequest.cpp \ src/common/NetworkRequest.cpp \
src/common/NetworkResult.cpp \
src/common/NetworkTimer.cpp \
src/controllers/accounts/Account.cpp \ src/controllers/accounts/Account.cpp \
src/controllers/accounts/AccountController.cpp \ src/controllers/accounts/AccountController.cpp \
src/controllers/accounts/AccountModel.cpp \ src/controllers/accounts/AccountModel.cpp \
@ -241,11 +242,13 @@ HEADERS += \
src/common/FlagsEnum.hpp \ src/common/FlagsEnum.hpp \
src/common/LockedObject.hpp \ src/common/LockedObject.hpp \
src/common/MutexValue.hpp \ src/common/MutexValue.hpp \
src/common/NetworkManager.hpp \ src/common/NetworkCommon.hpp \
src/common/NetworkResult.hpp \
src/common/NetworkData.hpp \ src/common/NetworkData.hpp \
src/common/NetworkManager.hpp \
src/common/NetworkRequest.hpp \ src/common/NetworkRequest.hpp \
src/common/NetworkRequester.hpp \ src/common/NetworkRequester.hpp \
src/common/NetworkResult.hpp \
src/common/NetworkTimer.hpp \
src/common/NetworkWorker.hpp \ src/common/NetworkWorker.hpp \
src/common/NullablePtr.hpp \ src/common/NullablePtr.hpp \
src/common/Property.hpp \ src/common/Property.hpp \

View file

@ -105,7 +105,6 @@ void NetworkRequest::execute()
// Get requests try to load from cache, then perform the request // Get requests try to load from cache, then perform the request
if (this->data->useQuickLoadCache_) { if (this->data->useQuickLoadCache_) {
if (this->tryLoadCachedFile()) { if (this->tryLoadCachedFile()) {
Log("Loaded from cache");
// Successfully loaded from cache // Successfully loaded from cache
return; return;
} }
@ -167,8 +166,7 @@ void NetworkRequest::doRequest()
this->timer->start(); this->timer->start();
auto onUrlRequested = [data = std::move(this->data), timer = std::move(this->timer), auto onUrlRequested = [data = this->data, timer = this->timer, worker]() mutable {
worker]() mutable {
QNetworkReply *reply = nullptr; QNetworkReply *reply = nullptr;
switch (data->requestType_) { switch (data->requestType_) {
case NetworkRequestType::Get: { case NetworkRequestType::Get: {
@ -205,7 +203,8 @@ void NetworkRequest::doRequest()
bool directAction = (data->caller_ == nullptr); bool directAction = (data->caller_ == nullptr);
auto handleReply = [data = std::move(data), timer = std::move(timer), reply]() mutable { auto handleReply = [data, timer, reply]() mutable {
// TODO(pajlada): A reply was received, kill the timeout timer
if (reply->error() != QNetworkReply::NetworkError::NoError) { if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data->onError_) { if (data->onError_) {
data->onError_(reply->error()); data->onError_(reply->error());
@ -225,7 +224,8 @@ void NetworkRequest::doRequest()
}; };
if (data->caller_ != nullptr) { if (data->caller_ != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl, data->caller_, std::move(handleReply)); QObject::connect(worker, &NetworkWorker::doneUrl, data->caller_,
std::move(handleReply));
QObject::connect(reply, &QNetworkReply::finished, worker, [worker]() mutable { QObject::connect(reply, &QNetworkReply::finished, worker, [worker]() mutable {
emit worker->doneUrl(); emit worker->doneUrl();
@ -241,7 +241,7 @@ void NetworkRequest::doRequest()
} }
}; };
QObject::connect(&requester, &NetworkRequester::requestUrl, worker, std::move(onUrlRequested)); QObject::connect(&requester, &NetworkRequester::requestUrl, worker, onUrlRequested);
emit requester.requestUrl(); emit requester.requestUrl();
} }

View file

@ -18,7 +18,7 @@ class NetworkRequest
// Timer that tracks the timeout // Timer that tracks the timeout
// By default, there's no explicit timeout for the request // By default, there's no explicit timeout for the request
// to enable the timer, the "setTimeout" function needs to be called before execute is called // to enable the timer, the "setTimeout" function needs to be called before execute is called
std::unique_ptr<NetworkTimer> timer; std::shared_ptr<NetworkTimer> timer;
// The NetworkRequest destructor will assert if executed_ hasn't been set to true before dying // The NetworkRequest destructor will assert if executed_ hasn't been set to true before dying
bool executed_ = false; bool executed_ = false;

View file

@ -0,0 +1,39 @@
#include "common/NetworkTimer.hpp"
#include "common/NetworkWorker.hpp"
#include <QTimer>
#include <cassert>
namespace chatterino {
void NetworkTimer::start()
{
if (this->timeoutMS_ <= 0) {
return;
}
this->timer_ = new QTimer;
this->timer_->start(this->timeoutMS_);
QObject::connect(this->timer_, &QTimer::timeout, [timer = this->timer_] {
timer->deleteLater(); //
});
this->started_ = true;
}
bool NetworkTimer::isStarted() const
{
return this->started_;
}
void NetworkTimer::onTimeout(NetworkWorker *worker, std::function<void()> cb) const
{
assert(this->timer_ != nullptr);
assert(worker != nullptr);
QObject::connect(this->timer_, &QTimer::timeout, worker, cb);
}
} // namespace chatterino

View file

@ -1,18 +1,16 @@
#pragma once #pragma once
#include "common/NetworkWorker.hpp"
#include <QTimer>
#include <cassert>
#include <functional> #include <functional>
#include <memory>
class QTimer;
namespace chatterino { namespace chatterino {
class NetworkWorker;
class NetworkTimer class NetworkTimer
{ {
std::unique_ptr<QTimer> timer_; QTimer *timer_ = nullptr;
bool started_{}; bool started_{};
@ -20,10 +18,7 @@ public:
int timeoutMS_ = -1; int timeoutMS_ = -1;
NetworkTimer() = default; NetworkTimer() = default;
~NetworkTimer() ~NetworkTimer() = default;
{
this->timer_.release();
}
NetworkTimer(const NetworkTimer &other) = delete; NetworkTimer(const NetworkTimer &other) = delete;
NetworkTimer &operator=(const NetworkTimer &other) = delete; NetworkTimer &operator=(const NetworkTimer &other) = delete;
@ -31,31 +26,11 @@ public:
NetworkTimer(NetworkTimer &&other) = default; NetworkTimer(NetworkTimer &&other) = default;
NetworkTimer &operator=(NetworkTimer &&other) = default; NetworkTimer &operator=(NetworkTimer &&other) = default;
void start() void start();
{
if (this->timeoutMS_ <= 0) {
return;
}
this->timer_ = std::make_unique<QTimer>(); void onTimeout(NetworkWorker *worker, std::function<void()> cb) const;
this->timer_->start(this->timeoutMS_);
this->started_ = true; bool isStarted() const;
}
bool isStarted() const
{
return this->started_;
}
void onTimeout(NetworkWorker *worker, std::function<void()> cb) const
{
if (!this->timer_) {
return;
}
QObject::connect(this->timer_.get(), &QTimer::timeout, worker, cb);
}
}; };
} // namespace chatterino } // namespace chatterino