From 3cea788fcf4411e139b988b0cd5440e883ebbfed Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Thu, 20 Feb 2025 22:41:53 +0800 Subject: [PATCH] opt:refactor wordfinder,use signal/connect to handle single requestFinish event. (#2151) * opt:refactor the wordfinder use more specific signal to handle the event * opt:check before invoke cancel --- src/article_netmgr.cc | 4 +++- src/wordfinder.cc | 39 ++++++++++++++++++++++++++++++++++++++- src/wordfinder.hh | 1 + 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/article_netmgr.cc b/src/article_netmgr.cc index 6af3f7d68..f3e03b2dc 100644 --- a/src/article_netmgr.cc +++ b/src/article_netmgr.cc @@ -288,7 +288,9 @@ ArticleResourceReply::ArticleResourceReply( QObject * parent, ArticleResourceReply::~ArticleResourceReply() { - req->cancel(); + if ( req ) { + req->cancel(); + } } void ArticleResourceReply::reqUpdated() diff --git a/src/wordfinder.cc b/src/wordfinder.cc index a6ec35887..b0315f419 100644 --- a/src/wordfinder.cc +++ b/src/wordfinder.cc @@ -139,7 +139,9 @@ void WordFinder::startSearch() inputDict->prefixMatch( allWordWriting, requestedMaxResults ) : inputDict->stemmedMatch( allWordWriting, stemmedMinLength, stemmedMaxSuffixVariation, requestedMaxResults ); - connect( sr.get(), &Dictionary::Request::finished, this, &WordFinder::requestFinished, Qt::QueuedConnection ); + connect( sr.get(), &Dictionary::Request::finished, this, [ this, sr ]() { + requestFinished( sr ); + } ); { QMutexLocker locker( &mutex ); @@ -218,6 +220,41 @@ void WordFinder::requestFinished() } } +void WordFinder::requestFinished( const sptr< Dictionary::WordSearchRequest > & req ) +{ + if ( !searchInProgress.load() ) { + return; + } + { + QMutexLocker locker( &mutex ); + queuedRequests.remove( req ); + + if ( req->isFinished() ) { + if ( !req->getErrorString().isEmpty() ) { + searchErrorString = tr( "Failed to query some dictionaries." ); + } + + if ( req->isUncertain() ) { + searchResultsUncertain = true; + } + + if ( req->matchesCount() > 0u ) { + // This list is handled by updateResults() + finishedRequests.push_back( req ); + } + } + } + + if ( !searchInProgress.load() ) { + return; + } + + if ( queuedRequests.empty() ) { + // Search is finished. + updateResults(); + } +} + namespace { diff --git a/src/wordfinder.hh b/src/wordfinder.hh index 6a41d1cfb..f073ea82d 100644 --- a/src/wordfinder.hh +++ b/src/wordfinder.hh @@ -129,6 +129,7 @@ public: /// requests exist, and hence no dictionaries are used anymore. Unlike /// cancel(), this may take some time to finish. void clear(); + void requestFinished( const sptr< Dictionary::WordSearchRequest > & ); signals: /// Indicates that the search has got some more results, and continues