fix(RD): separate search and versions/info tasks

This allows us to check whether a search request is already on-going, in
which case we don't need to make another one (and shouldn't).

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-12-20 17:14:17 -03:00
parent 38e20eb148
commit 563fe8d515
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
4 changed files with 49 additions and 15 deletions

View File

@ -101,7 +101,7 @@ void ModPage::triggerSearch()
} }
static_cast<ModModel*>(m_model)->searchWithTerm(getSearchTerm(), m_ui->sortByBox->currentData().toUInt(), changed); static_cast<ModModel*>(m_model)->searchWithTerm(getSearchTerm(), m_ui->sortByBox->currentData().toUInt(), changed);
m_fetch_progress.watch(&m_model->activeJob()); m_fetch_progress.watch(m_model->activeSearchJob().get());
} }
QMap<QString, QString> ModPage::urlHandlers() const QMap<QString, QString> ModPage::urlHandlers() const

View File

@ -123,8 +123,8 @@ void ResourceModel::fetchMore(const QModelIndex& parent)
void ResourceModel::search() void ResourceModel::search()
{ {
if (!m_current_job.isRunning()) if (hasActiveSearchJob())
m_current_job.clear(); return;
auto args{ createSearchArguments() }; auto args{ createSearchArguments() };
@ -146,22 +146,22 @@ void ResourceModel::search()
}; };
if (auto job = m_api->searchProjects(std::move(args), std::move(callbacks)); job) if (auto job = m_api->searchProjects(std::move(args), std::move(callbacks)); job)
addActiveJob(job); runSearchJob(job);
} }
void ResourceModel::loadEntry(QModelIndex& entry) void ResourceModel::loadEntry(QModelIndex& entry)
{ {
auto const& pack = m_packs[entry.row()]; auto const& pack = m_packs[entry.row()];
if (!m_current_job.isRunning()) if (!hasActiveInfoJob())
m_current_job.clear(); m_current_info_job.clear();
if (!pack.versionsLoaded) { if (!pack.versionsLoaded) {
auto args{ createVersionsArguments(entry) }; auto args{ createVersionsArguments(entry) };
auto callbacks{ createVersionsCallbacks(entry) }; auto callbacks{ createVersionsCallbacks(entry) };
if (auto job = m_api->getProjectVersions(std::move(args), std::move(callbacks)); job) if (auto job = m_api->getProjectVersions(std::move(args), std::move(callbacks)); job)
addActiveJob(job); runInfoJob(job);
} }
if (!pack.extraDataLoaded) { if (!pack.extraDataLoaded) {
@ -169,14 +169,25 @@ void ResourceModel::loadEntry(QModelIndex& entry)
auto callbacks{ createInfoCallbacks(entry) }; auto callbacks{ createInfoCallbacks(entry) };
if (auto job = m_api->getProjectInfo(std::move(args), std::move(callbacks)); job) if (auto job = m_api->getProjectInfo(std::move(args), std::move(callbacks)); job)
addActiveJob(job); runInfoJob(job);
} }
} }
void ResourceModel::refresh() void ResourceModel::refresh()
{ {
if (m_current_job.isRunning()) { bool reset_requested = false;
m_current_job.abort();
if (hasActiveInfoJob()) {
m_current_info_job.abort();
reset_requested = true;
}
if (hasActiveSearchJob()) {
m_current_search_job->abort();
reset_requested = true;
}
if (reset_requested) {
m_search_state = SearchState::ResetRequested; m_search_state = SearchState::ResetRequested;
return; return;
} }
@ -195,6 +206,22 @@ void ResourceModel::clearData()
endResetModel(); endResetModel();
} }
void ResourceModel::runSearchJob(NetJob::Ptr ptr)
{
m_current_search_job = ptr;
m_current_search_job->start();
}
void ResourceModel::runInfoJob(Task::Ptr ptr)
{
if (!m_current_info_job.isRunning())
m_current_info_job.clear();
m_current_info_job.addTask(ptr);
if (!m_current_info_job.isRunning())
m_current_info_job.run();
}
std::optional<QIcon> ResourceModel::getIcon(QModelIndex& index, const QUrl& url) std::optional<QIcon> ResourceModel::getIcon(QModelIndex& index, const QUrl& url)
{ {
QPixmap pixmap; QPixmap pixmap;

View File

@ -40,8 +40,9 @@ class ResourceModel : public QAbstractListModel {
[[nodiscard]] inline int columnCount(const QModelIndex& parent) const override { return parent.isValid() ? 0 : 1; } [[nodiscard]] inline int columnCount(const QModelIndex& parent) const override { return parent.isValid() ? 0 : 1; }
[[nodiscard]] inline auto flags(const QModelIndex& index) const -> Qt::ItemFlags override { return QAbstractListModel::flags(index); } [[nodiscard]] inline auto flags(const QModelIndex& index) const -> Qt::ItemFlags override { return QAbstractListModel::flags(index); }
inline void addActiveJob(Task::Ptr ptr) { m_current_job.addTask(ptr); if (!m_current_job.isRunning()) m_current_job.start(); } [[nodiscard]] bool hasActiveSearchJob() const { return m_current_search_job && m_current_search_job->isRunning(); }
inline Task const& activeJob() { return m_current_job; } [[nodiscard]] bool hasActiveInfoJob() const { return m_current_info_job.isRunning(); }
[[nodiscard]] Task::Ptr activeSearchJob() { return hasActiveSearchJob() ? m_current_search_job : nullptr; }
[[nodiscard]] auto getSortingMethods() const { return m_api->getSortingMethods(); } [[nodiscard]] auto getSortingMethods() const { return m_api->getSortingMethods(); }
@ -80,6 +81,9 @@ class ResourceModel : public QAbstractListModel {
/** Resets the model's data. */ /** Resets the model's data. */
void clearData(); void clearData();
void runSearchJob(NetJob::Ptr);
void runInfoJob(Task::Ptr);
protected: protected:
const BaseInstance& m_base_instance; const BaseInstance& m_base_instance;
@ -91,7 +95,10 @@ class ResourceModel : public QAbstractListModel {
std::unique_ptr<ResourceAPI> m_api; std::unique_ptr<ResourceAPI> m_api;
ConcurrentTask m_current_job; // Job for searching for new entries
shared_qobject_ptr<NetJob> m_current_search_job;
// Job for fetching versions and extra info on existing entries
ConcurrentTask m_current_info_job;
shared_qobject_ptr<NetJob> m_current_icon_job; shared_qobject_ptr<NetJob> m_current_icon_job;
QSet<QUrl> m_currently_running_icon_actions; QSet<QUrl> m_currently_running_icon_actions;

View File

@ -353,8 +353,8 @@ void ResourcePage::openUrl(const QUrl& url)
searchEdit->setText(slug); searchEdit->setText(slug);
newPage->triggerSearch(); newPage->triggerSearch();
if (model->activeJob().isRunning()) if (model->hasActiveSearchJob())
connect(&model->activeJob(), &Task::finished, jump); connect(model->activeSearchJob().get(), &Task::finished, jump);
else else
jump(); jump();