From a841c776d30e705a18800269940b53cc1bb00457 Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Tue, 2 Sep 2025 17:24:14 +1000 Subject: plugin/fsm: async add watcher paths Also remove debug messages and remove futures from hash on completion --- plugin/src/Caelestia/filesystemmodel.cpp | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/plugin/src/Caelestia/filesystemmodel.cpp b/plugin/src/Caelestia/filesystemmodel.cpp index ee2f524..40b322b 100644 --- a/plugin/src/Caelestia/filesystemmodel.cpp +++ b/plugin/src/Caelestia/filesystemmodel.cpp @@ -80,10 +80,25 @@ QList FileSystemModel::entries() const { void FileSystemModel::watchDirIfRecursive(const QString& path) { if (m_recursive) { - QDirIterator iter(path, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); - while (iter.hasNext()) { - m_watcher.addPath(iter.next()); - } + const auto currentDir = m_dir; + const auto future = QtConcurrent::run([path]() { + QDirIterator iter(path, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); + QStringList dirs; + while (iter.hasNext()) { + dirs << iter.next(); + } + return dirs; + }); + const auto watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, [currentDir, watcher, this]() { + const auto paths = watcher->result(); + if (currentDir == m_dir && !paths.isEmpty()) { + // Ignore if dir has changed + m_watcher.addPaths(paths); + } + watcher->deleteLater(); + }); + watcher->setFuture(future); } } @@ -188,15 +203,14 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) { const auto watcher = new QFutureWatcher, QSet>>(this); - connect(watcher, &QFutureWatcher, QSet>>::finished, this, [watcher, this]() { + connect(watcher, &QFutureWatcher, QSet>>::finished, this, [dir, watcher, this]() { + m_futures.remove(dir); + if (!watcher->future().isResultReadyAt(0)) { watcher->deleteLater(); return; } - QElapsedTimer timer; - timer.start(); - const auto result = watcher->result(); const auto removedPaths = result.first; const auto addedPaths = result.second; @@ -229,8 +243,6 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) { endResetModel(); watcher->deleteLater(); - - qDebug() << "Update took" << timer.elapsed() << "ms"; }); watcher->setFuture(future); -- cgit v1.2.3-freya