Commit 5171bc3c authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Dictionary runner: initialize when needed

Avoid initalizing on startup, its costly and uneeded
parent fd8934ad
Pipeline #137900 passed with stage
in 3 minutes and 4 seconds
......@@ -7,16 +7,20 @@
#include <KLocalizedString>
#include <QEventLoop>
#include <QMutex>
#include <QMutexLocker>
#include <QStringList>
#include <QTimer>
static const char CONFIG_TRIGGERWORD[] = "triggerWord";
namespace
{
const char CONFIG_TRIGGERWORD[] = "triggerWord";
QMutex s_initMutex;
}
DictionaryRunner::DictionaryRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
: AbstractRunner(parent, metaData, args)
{
m_engine = new DictionaryMatchEngine(m_consumer.dataEngine(QStringLiteral("dict")), this);
setPriority(LowPriority);
setObjectName(QLatin1String("Dictionary"));
}
......@@ -48,6 +52,25 @@ void DictionaryRunner::match(RunnerContext &context)
if (query.isEmpty()) {
return;
}
// Initialize engine
{
// It can happen that we are in this function and
// another match starts happening. Hence we lock to
// ensure that we always init the engine once
QMutexLocker lock(&s_initMutex);
if (!m_engine) {
QMetaObject::invokeMethod(
this,
[this] {
m_consumer = std::make_unique<Plasma::DataEngineConsumer>();
m_engine = std::make_unique<DictionaryMatchEngine>(m_consumer->dataEngine(QStringLiteral("dict")));
},
Qt::BlockingQueuedConnection);
}
}
QEventLoop loop;
QTimer::singleShot(400, &loop, [&loop]() {
loop.quit();
......
......@@ -23,8 +23,8 @@ public:
private:
QString m_triggerWord;
DictionaryMatchEngine *m_engine;
Plasma::DataEngineConsumer m_consumer;
std::unique_ptr<DictionaryMatchEngine> m_engine;
std::unique_ptr<Plasma::DataEngineConsumer> m_consumer;
protected Q_SLOTS:
void init() override;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment