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);
m_fetch_progress.watch(&m_model->activeJob());
m_fetch_progress.watch(m_model->activeSearchJob().get());
}
QMap<QString, QString> ModPage::urlHandlers() const

View File

@ -123,8 +123,8 @@ void ResourceModel::fetchMore(const QModelIndex& parent)
void ResourceModel::search()
{
if (!m_current_job.isRunning())
m_current_job.clear();
if (hasActiveSearchJob())
return;
auto args{ createSearchArguments() };
@ -146,22 +146,22 @@ void ResourceModel::search()
};
if (auto job = m_api->searchProjects(std::move(args), std::move(callbacks)); job)
addActiveJob(job);
runSearchJob(job);
}
void ResourceModel::loadEntry(QModelIndex& entry)
{
auto const& pack = m_packs[entry.row()];
if (!m_current_job.isRunning())
m_current_job.clear();
if (!hasActiveInfoJob())
m_current_info_job.clear();
if (!pack.versionsLoaded) {
auto args{ createVersionsArguments(entry) };
auto callbacks{ createVersionsCallbacks(entry) };
if (auto job = m_api->getProjectVersions(std::move(args), std::move(callbacks)); job)
addActiveJob(job);
runInfoJob(job);
}
if (!pack.extraDataLoaded) {
@ -169,14 +169,25 @@ void ResourceModel::loadEntry(QModelIndex& entry)
auto callbacks{ createInfoCallbacks(entry) };
if (auto job = m_api->getProjectInfo(std::move(args), std::move(callbacks)); job)
addActiveJob(job);
runInfoJob(job);
}
}
void ResourceModel::refresh()
{
if (m_current_job.isRunning()) {
m_current_job.abort();
bool reset_requested = false;
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;
return;
}
@ -195,6 +206,22 @@ void ResourceModel::clearData()
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)
{
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 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(); }
inline Task const& activeJob() { return m_current_job; }
[[nodiscard]] bool hasActiveSearchJob() const { return m_current_search_job && m_current_search_job->isRunning(); }
[[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(); }
@ -80,6 +81,9 @@ class ResourceModel : public QAbstractListModel {
/** Resets the model's data. */
void clearData();
void runSearchJob(NetJob::Ptr);
void runInfoJob(Task::Ptr);
protected:
const BaseInstance& m_base_instance;
@ -91,7 +95,10 @@ class ResourceModel : public QAbstractListModel {
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;
QSet<QUrl> m_currently_running_icon_actions;

View File

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