Skip to content

Commit

Permalink
opt:refactor wordfinder,use signal/connect to handle single requestFi…
Browse files Browse the repository at this point in the history
…nish event. (#2151)

* opt:refactor the wordfinder

use more specific signal to handle the event

* opt:check before invoke cancel
  • Loading branch information
xiaoyifang authored Feb 20, 2025
1 parent a40a746 commit 3cea788
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/article_netmgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ ArticleResourceReply::ArticleResourceReply( QObject * parent,

ArticleResourceReply::~ArticleResourceReply()
{
req->cancel();
if ( req ) {
req->cancel();
}
}

void ArticleResourceReply::reqUpdated()
Expand Down
39 changes: 38 additions & 1 deletion src/wordfinder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 {


Expand Down
1 change: 1 addition & 0 deletions src/wordfinder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cea788

Please sign in to comment.