mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Fix timer stuff (#580)
* Add and sort Network files to chatterino project file
This commit is contained in:
parent
55269587f5
commit
2ea3643100
|
@ -102,10 +102,11 @@ SOURCES += \
|
|||
src/common/Channel.cpp \
|
||||
src/common/CompletionModel.cpp \
|
||||
src/common/Emotemap.cpp \
|
||||
src/common/NetworkManager.cpp \
|
||||
src/common/NetworkResult.cpp \
|
||||
src/common/NetworkData.cpp \
|
||||
src/common/NetworkManager.cpp \
|
||||
src/common/NetworkRequest.cpp \
|
||||
src/common/NetworkResult.cpp \
|
||||
src/common/NetworkTimer.cpp \
|
||||
src/controllers/accounts/Account.cpp \
|
||||
src/controllers/accounts/AccountController.cpp \
|
||||
src/controllers/accounts/AccountModel.cpp \
|
||||
|
@ -241,11 +242,13 @@ HEADERS += \
|
|||
src/common/FlagsEnum.hpp \
|
||||
src/common/LockedObject.hpp \
|
||||
src/common/MutexValue.hpp \
|
||||
src/common/NetworkManager.hpp \
|
||||
src/common/NetworkResult.hpp \
|
||||
src/common/NetworkCommon.hpp \
|
||||
src/common/NetworkData.hpp \
|
||||
src/common/NetworkManager.hpp \
|
||||
src/common/NetworkRequest.hpp \
|
||||
src/common/NetworkRequester.hpp \
|
||||
src/common/NetworkResult.hpp \
|
||||
src/common/NetworkTimer.hpp \
|
||||
src/common/NetworkWorker.hpp \
|
||||
src/common/NullablePtr.hpp \
|
||||
src/common/Property.hpp \
|
||||
|
|
|
@ -105,7 +105,6 @@ void NetworkRequest::execute()
|
|||
// Get requests try to load from cache, then perform the request
|
||||
if (this->data->useQuickLoadCache_) {
|
||||
if (this->tryLoadCachedFile()) {
|
||||
Log("Loaded from cache");
|
||||
// Successfully loaded from cache
|
||||
return;
|
||||
}
|
||||
|
@ -167,8 +166,7 @@ void NetworkRequest::doRequest()
|
|||
|
||||
this->timer->start();
|
||||
|
||||
auto onUrlRequested = [data = std::move(this->data), timer = std::move(this->timer),
|
||||
worker]() mutable {
|
||||
auto onUrlRequested = [data = this->data, timer = this->timer, worker]() mutable {
|
||||
QNetworkReply *reply = nullptr;
|
||||
switch (data->requestType_) {
|
||||
case NetworkRequestType::Get: {
|
||||
|
@ -205,7 +203,8 @@ void NetworkRequest::doRequest()
|
|||
|
||||
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 (data->onError_) {
|
||||
data->onError_(reply->error());
|
||||
|
@ -225,7 +224,8 @@ void NetworkRequest::doRequest()
|
|||
};
|
||||
|
||||
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 {
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ class NetworkRequest
|
|||
// Timer that tracks the timeout
|
||||
// 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
|
||||
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
|
||||
bool executed_ = false;
|
||||
|
|
39
src/common/NetworkTimer.cpp
Normal file
39
src/common/NetworkTimer.cpp
Normal 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
|
|
@ -1,18 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "common/NetworkWorker.hpp"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
class QTimer;
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
class NetworkWorker;
|
||||
|
||||
class NetworkTimer
|
||||
{
|
||||
std::unique_ptr<QTimer> timer_;
|
||||
QTimer *timer_ = nullptr;
|
||||
|
||||
bool started_{};
|
||||
|
||||
|
@ -20,10 +18,7 @@ public:
|
|||
int timeoutMS_ = -1;
|
||||
|
||||
NetworkTimer() = default;
|
||||
~NetworkTimer()
|
||||
{
|
||||
this->timer_.release();
|
||||
}
|
||||
~NetworkTimer() = default;
|
||||
|
||||
NetworkTimer(const NetworkTimer &other) = delete;
|
||||
NetworkTimer &operator=(const NetworkTimer &other) = delete;
|
||||
|
@ -31,31 +26,11 @@ public:
|
|||
NetworkTimer(NetworkTimer &&other) = default;
|
||||
NetworkTimer &operator=(NetworkTimer &&other) = default;
|
||||
|
||||
void start()
|
||||
{
|
||||
if (this->timeoutMS_ <= 0) {
|
||||
return;
|
||||
}
|
||||
void start();
|
||||
|
||||
this->timer_ = std::make_unique<QTimer>();
|
||||
this->timer_->start(this->timeoutMS_);
|
||||
void onTimeout(NetworkWorker *worker, std::function<void()> cb) const;
|
||||
|
||||
this->started_ = true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
bool isStarted() const;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
Loading…
Reference in a new issue