mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Trying out a solution KKona
This commit is contained in:
parent
6bf7ba1ebe
commit
55269587f5
|
@ -12,17 +12,19 @@
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
|
|
||||||
NetworkRequest::NetworkRequest(const std::string &url, NetworkRequestType requestType)
|
NetworkRequest::NetworkRequest(const std::string &url, NetworkRequestType requestType)
|
||||||
: timer(new NetworkTimer)
|
: data(new NetworkData)
|
||||||
|
, timer(new NetworkTimer)
|
||||||
{
|
{
|
||||||
this->data.request_.setUrl(QUrl(QString::fromStdString(url)));
|
this->data->request_.setUrl(QUrl(QString::fromStdString(url)));
|
||||||
this->data.requestType_ = requestType;
|
this->data->requestType_ = requestType;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
|
NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
|
||||||
: timer(new NetworkTimer)
|
: data(new NetworkData)
|
||||||
|
, timer(new NetworkTimer)
|
||||||
{
|
{
|
||||||
this->data.request_.setUrl(url);
|
this->data->request_.setUrl(url);
|
||||||
this->data.requestType_ = requestType;
|
this->data->requestType_ = requestType;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkRequest::~NetworkRequest()
|
NetworkRequest::~NetworkRequest()
|
||||||
|
@ -32,42 +34,42 @@ NetworkRequest::~NetworkRequest()
|
||||||
|
|
||||||
void NetworkRequest::setRequestType(NetworkRequestType newRequestType)
|
void NetworkRequest::setRequestType(NetworkRequestType newRequestType)
|
||||||
{
|
{
|
||||||
this->data.requestType_ = newRequestType;
|
this->data->requestType_ = newRequestType;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::setCaller(const QObject *caller)
|
void NetworkRequest::setCaller(const QObject *caller)
|
||||||
{
|
{
|
||||||
this->data.caller_ = caller;
|
this->data->caller_ = caller;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb)
|
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb)
|
||||||
{
|
{
|
||||||
this->data.onReplyCreated_ = cb;
|
this->data->onReplyCreated_ = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::onError(NetworkErrorCallback cb)
|
void NetworkRequest::onError(NetworkErrorCallback cb)
|
||||||
{
|
{
|
||||||
this->data.onError_ = cb;
|
this->data->onError_ = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::onSuccess(NetworkSuccessCallback cb)
|
void NetworkRequest::onSuccess(NetworkSuccessCallback cb)
|
||||||
{
|
{
|
||||||
this->data.onSuccess_ = cb;
|
this->data->onSuccess_ = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
|
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
|
||||||
{
|
{
|
||||||
this->data.request_.setRawHeader(headerName, value);
|
this->data->request_.setRawHeader(headerName, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value)
|
void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value)
|
||||||
{
|
{
|
||||||
this->data.request_.setRawHeader(headerName, value);
|
this->data->request_.setRawHeader(headerName, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
|
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
|
||||||
{
|
{
|
||||||
this->data.request_.setRawHeader(headerName, value.toUtf8());
|
this->data->request_.setRawHeader(headerName, value.toUtf8());
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::setTimeout(int ms)
|
void NetworkRequest::setTimeout(int ms)
|
||||||
|
@ -86,22 +88,22 @@ void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oa
|
||||||
|
|
||||||
void NetworkRequest::setPayload(const QByteArray &payload)
|
void NetworkRequest::setPayload(const QByteArray &payload)
|
||||||
{
|
{
|
||||||
this->data.payload_ = payload;
|
this->data->payload_ = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::setUseQuickLoadCache(bool value)
|
void NetworkRequest::setUseQuickLoadCache(bool value)
|
||||||
{
|
{
|
||||||
this->data.useQuickLoadCache_ = value;
|
this->data->useQuickLoadCache_ = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRequest::execute()
|
void NetworkRequest::execute()
|
||||||
{
|
{
|
||||||
this->executed_ = true;
|
this->executed_ = true;
|
||||||
|
|
||||||
switch (this->data.requestType_) {
|
switch (this->data->requestType_) {
|
||||||
case NetworkRequestType::Get: {
|
case NetworkRequestType::Get: {
|
||||||
// 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");
|
Log("Loaded from cache");
|
||||||
// Successfully loaded from cache
|
// Successfully loaded from cache
|
||||||
|
@ -132,7 +134,7 @@ bool NetworkRequest::tryLoadCachedFile()
|
||||||
{
|
{
|
||||||
auto app = getApp();
|
auto app = getApp();
|
||||||
|
|
||||||
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash());
|
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data->getHash());
|
||||||
|
|
||||||
if (!cachedFile.exists()) {
|
if (!cachedFile.exists()) {
|
||||||
// File didn't exist
|
// File didn't exist
|
||||||
|
@ -147,7 +149,7 @@ bool NetworkRequest::tryLoadCachedFile()
|
||||||
QByteArray bytes = cachedFile.readAll();
|
QByteArray bytes = cachedFile.readAll();
|
||||||
NetworkResult result(bytes);
|
NetworkResult result(bytes);
|
||||||
|
|
||||||
bool success = this->data.onSuccess_(result);
|
bool success = this->data->onSuccess_(result);
|
||||||
|
|
||||||
cachedFile.close();
|
cachedFile.close();
|
||||||
|
|
||||||
|
@ -168,17 +170,17 @@ void NetworkRequest::doRequest()
|
||||||
auto onUrlRequested = [data = std::move(this->data), timer = std::move(this->timer),
|
auto onUrlRequested = [data = std::move(this->data), timer = std::move(this->timer),
|
||||||
worker]() mutable {
|
worker]() mutable {
|
||||||
QNetworkReply *reply = nullptr;
|
QNetworkReply *reply = nullptr;
|
||||||
switch (data.requestType_) {
|
switch (data->requestType_) {
|
||||||
case NetworkRequestType::Get: {
|
case NetworkRequestType::Get: {
|
||||||
reply = NetworkManager::NaM.get(data.request_);
|
reply = NetworkManager::NaM.get(data->request_);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case NetworkRequestType::Put: {
|
case NetworkRequestType::Put: {
|
||||||
reply = NetworkManager::NaM.put(data.request_, data.payload_);
|
reply = NetworkManager::NaM.put(data->request_, data->payload_);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case NetworkRequestType::Delete: {
|
case NetworkRequestType::Delete: {
|
||||||
reply = NetworkManager::NaM.deleteResource(data.request_);
|
reply = NetworkManager::NaM.deleteResource(data->request_);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,22 +193,22 @@ void NetworkRequest::doRequest()
|
||||||
timer->onTimeout(worker, [reply, data]() {
|
timer->onTimeout(worker, [reply, data]() {
|
||||||
Log("Aborted!");
|
Log("Aborted!");
|
||||||
reply->abort();
|
reply->abort();
|
||||||
if (data.onError_) {
|
if (data->onError_) {
|
||||||
data.onError_(-2);
|
data->onError_(-2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.onReplyCreated_) {
|
if (data->onReplyCreated_) {
|
||||||
data.onReplyCreated_(reply);
|
data->onReplyCreated_(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = std::move(data), timer = std::move(timer), reply]() mutable {
|
||||||
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());
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -214,16 +216,16 @@ void NetworkRequest::doRequest()
|
||||||
QByteArray readBytes = reply->readAll();
|
QByteArray readBytes = reply->readAll();
|
||||||
QByteArray bytes;
|
QByteArray bytes;
|
||||||
bytes.setRawData(readBytes.data(), readBytes.size());
|
bytes.setRawData(readBytes.data(), readBytes.size());
|
||||||
data.writeToCache(bytes);
|
data->writeToCache(bytes);
|
||||||
|
|
||||||
NetworkResult result(bytes);
|
NetworkResult result(bytes);
|
||||||
data.onSuccess_(result);
|
data->onSuccess_(result);
|
||||||
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
};
|
};
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace chatterino {
|
||||||
class NetworkRequest
|
class NetworkRequest
|
||||||
{
|
{
|
||||||
// Stores all data about the request that needs to be passed around to each part of the request
|
// Stores all data about the request that needs to be passed around to each part of the request
|
||||||
NetworkData data;
|
std::shared_ptr<NetworkData> data;
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -20,7 +20,10 @@ public:
|
||||||
int timeoutMS_ = -1;
|
int timeoutMS_ = -1;
|
||||||
|
|
||||||
NetworkTimer() = default;
|
NetworkTimer() = default;
|
||||||
~NetworkTimer() = default;
|
~NetworkTimer()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
Loading…
Reference in a new issue