made caller/concurrent rules tighter
This commit is contained in:
@@ -125,6 +125,11 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
|
||||
}
|
||||
|
||||
auto handleReply = [data, reply]() mutable {
|
||||
if (data->hasCaller_ && !data->caller_.get())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(pajlada): A reply was received, kill the timeout timer
|
||||
if (reply->error() != QNetworkReply::NetworkError::NoError)
|
||||
{
|
||||
@@ -162,7 +167,6 @@ void loadUncached(const std::shared_ptr<NetworkData> &data)
|
||||
if (data->executeConcurrently || isGuiThread())
|
||||
{
|
||||
handleReply();
|
||||
|
||||
delete worker;
|
||||
}
|
||||
else
|
||||
@@ -206,11 +210,22 @@ void loadCached(const std::shared_ptr<NetworkData> &data)
|
||||
// XXX: If outcome is Failure, we should invalidate the cache file
|
||||
// somehow/somewhere
|
||||
/*auto outcome =*/
|
||||
if (data->hasCaller_ && !data->caller_.get())
|
||||
{
|
||||
return;
|
||||
}
|
||||
data->onSuccess_(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
postToThread([data, result]() { data->onSuccess_(result); });
|
||||
postToThread([data, result]() {
|
||||
if (data->hasCaller_ && !data->caller_.get())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
data->onSuccess_(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/NetworkCommon.hpp"
|
||||
#include "util/QObjectRef.hpp"
|
||||
|
||||
#include <QNetworkRequest>
|
||||
#include <functional>
|
||||
@@ -32,7 +33,8 @@ struct NetworkData {
|
||||
~NetworkData();
|
||||
|
||||
QNetworkRequest request_;
|
||||
const QObject *caller_ = nullptr;
|
||||
bool hasCaller_{};
|
||||
QObjectRef<QObject> caller_;
|
||||
bool useQuickLoadCache_{};
|
||||
bool executeConcurrently{};
|
||||
|
||||
|
||||
@@ -50,7 +50,11 @@ NetworkRequest NetworkRequest::type(NetworkRequestType newRequestType) &&
|
||||
|
||||
NetworkRequest NetworkRequest::caller(const QObject *caller) &&
|
||||
{
|
||||
this->data->caller_ = caller;
|
||||
// Caller must be in gui thread
|
||||
assert(caller->thread() == qApp->thread());
|
||||
|
||||
this->data->caller_ = const_cast<QObject *>(caller);
|
||||
this->data->hasCaller_ = true;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
@@ -144,6 +148,9 @@ void NetworkRequest::execute()
|
||||
this->data->useQuickLoadCache_ = false;
|
||||
}
|
||||
|
||||
// Can not have a caller and be concurrent at the same time.
|
||||
assert(!(this->data->caller_ && this->data->executeConcurrently));
|
||||
|
||||
load(std::move(this->data));
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ public:
|
||||
|
||||
NetworkRequest payload(const QByteArray &payload) &&;
|
||||
NetworkRequest cache() &&;
|
||||
/// NetworkRequest makes sure that the `caller` object still exists when the
|
||||
/// callbacks are executed. Cannot be used with concurrent() since we can't
|
||||
/// make sure that the object doesn't get deleted while the callback is
|
||||
/// running.
|
||||
NetworkRequest caller(const QObject *caller) &&;
|
||||
NetworkRequest header(const char *headerName, const char *value) &&;
|
||||
NetworkRequest header(const char *headerName, const QByteArray &value) &&;
|
||||
|
||||
Reference in New Issue
Block a user