Skip to content

Commit

Permalink
refactor(core,registry): use std::async instead of QtConcurrent::run
Browse files Browse the repository at this point in the history
  • Loading branch information
trollixx committed Jul 19, 2021
1 parent 8854bf5 commit dbb8eb2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
23 changes: 10 additions & 13 deletions src/libs/core/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <QFutureWatcher>
#include <QLoggingCategory>

#include <QtConcurrent>
#include <future>

using namespace Zeal::Core;

Expand Down Expand Up @@ -64,20 +64,17 @@ bool FileManager::removeRecursively(const QString &path)

qCDebug(log, "Renamed '%s' to '%s'.", qPrintable(path), qPrintable(deletePath));

auto watcher = new QFutureWatcher<bool>();
connect(watcher, &QFutureWatcher<bool>::finished, [=] {
if (!watcher->result()) {
qCWarning(log, "Failed to remove '%s'.", qPrintable(deletePath));
} else {
qCDebug(log, "Removed '%s'.", qPrintable(deletePath));
}

watcher->deleteLater();
std::future<bool> f = std::async(std::launch::async, [deletePath](){
return QDir(deletePath).removeRecursively();
});

watcher->setFuture(QtConcurrent::run([deletePath] {
return QDir(deletePath).removeRecursively();
}));
f.wait();

if (!f.get()) {
qCWarning(log, "Failed to remove '%s'.", qPrintable(deletePath));
} else {
qCDebug(log, "Removed '%s'.", qPrintable(deletePath));
}

return true;
}
61 changes: 29 additions & 32 deletions src/libs/registry/docsetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <QtConcurrent>

#include <functional>
#include <future>

using namespace Zeal::Registry;

Expand Down Expand Up @@ -122,45 +123,41 @@ QStringList DocsetRegistry::names() const

void DocsetRegistry::loadDocset(const QString &path)
{
auto watcher = new QFutureWatcher<Docset *>();
connect(watcher, &QFutureWatcher<Docset *>::finished, this, [this, watcher] {
QScopedPointer<QFutureWatcher<Docset *>, QScopedPointerDeleteLater> guard(watcher);

Docset *docset = watcher->result();
// TODO: Emit error
if (!docset->isValid()) {
qWarning("Could not load docset from '%s'. Reinstall the docset.",
qPrintable(docset->path()));
delete docset;
return;
}
std::future<Docset *> f = std::async(std::launch::async, [path](){
return new Docset(path);
});

docset->setFuzzySearchEnabled(m_fuzzySearchEnabled);
f.wait();
Docset *docset = f.get();
// TODO: Emit error
if (!docset->isValid()) {
qWarning("Could not load docset from '%s'. Reinstall the docset.",
qPrintable(docset->path()));
delete docset;
return;
}

const QString name = docset->name();
if (m_docsets.contains(name)) {
unloadDocset(name);
}
docset->setFuzzySearchEnabled(m_fuzzySearchEnabled);

// Setup HTTP mount.
QUrl url = Core::Application::instance()->httpServer()->mount(name, docset->documentPath());
if (url.isEmpty()) {
qWarning("Could not enable docset from '%s'. Reinstall the docset.",
qPrintable(docset->path()));
delete docset;
return;
}
const QString name = docset->name();
if (m_docsets.contains(name)) {
unloadDocset(name);
}

docset->setBaseUrl(url);
// Setup HTTP mount.
QUrl url = Core::Application::instance()->httpServer()->mount(name, docset->documentPath());
if (url.isEmpty()) {
qWarning("Could not enable docset from '%s'. Reinstall the docset.",
qPrintable(docset->path()));
delete docset;
return;
}

m_docsets[name] = docset;
docset->setBaseUrl(url);

emit docsetLoaded(name);
});
m_docsets[name] = docset;

watcher->setFuture(QtConcurrent::run([path] {
return new Docset(path);
}));
emit docsetLoaded(name);
}

void DocsetRegistry::unloadDocset(const QString &name)
Expand Down

0 comments on commit dbb8eb2

Please sign in to comment.