Skip to content

Commit

Permalink
Run page parsing in separate thread (issue #1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed May 16, 2018
1 parent ff10f6d commit d2d7995
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions gui/src/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "downloader/downloader.h"
#include "functions.h"
#include "mainwindow.h"
#include "models/page-api.h"
#include "models/profile.h"
#include "models/site.h"
#include "updater/update-dialog.h"
Expand All @@ -52,6 +53,8 @@ int main(int argc, char *argv[])
app.setOrganizationName("Bionus");
app.setOrganizationDomain("bionus.fr.cr");

qRegisterMetaType<PageApi::LoadResult>("LoadResult");

// Set window title according to the current build
#ifdef NIGHTLY
QString commit(NIGHTLY_COMMIT);
Expand Down
1 change: 1 addition & 0 deletions lib/src/models/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ QSharedPointer<Image> Api::parseImage(Page *parentPage, QMap<QString, QString> d

// Generate image
QSharedPointer<Image> img(new Image(site, d, site->getSource()->getProfile(), parentPage));
img->moveToThread(this->thread());
if (!tags.isEmpty())
img->setTags(tags);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/models/api/javascript-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ ParsedPage JavascriptApi::parsePage(Page *parentPage, const QString &source, int

ParsedPage ret;

m_engineMutex.lock();
Site *site = parentPage->site();
const QJSValue &api = m_source.property("apis").property(m_key);
QJSValue parseFunction = api.property("search").property("parse");
const QJSValue &results = parseFunction.call(QList<QJSValue>() << source);
m_engineMutex.unlock();

// Script errors and exceptions
if (results.isError())
Expand Down
2 changes: 2 additions & 0 deletions lib/src/models/api/javascript-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define JAVASCRIPT_API_H

#include <QJSValue>
#include <QMutex>
#include "models/api/api.h"


Expand Down Expand Up @@ -40,6 +41,7 @@ class JavascriptApi : public Api
private:
const QJSValue &m_source;
QString m_key;
mutable QMutex m_engineMutex;
};

#endif // JAVASCRIPT_API_H
3 changes: 2 additions & 1 deletion lib/src/models/page-api.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "models/page-api.h"
#include <QDomDocument>
#include <QRegularExpression>
#include <QtConcurrentRun>
#include <QtMath>
#include "functions.h"
#include "logger.h"
Expand Down Expand Up @@ -159,7 +160,7 @@ void PageApi::parse()
return;
}

parseActual();
QtConcurrent::run(this, &PageApi::parseActual);
}

void PageApi::parseActual()
Expand Down

0 comments on commit d2d7995

Please sign in to comment.