summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/clientapi.cpp14
-rw-r--r--common/clientapi.h8
-rw-r--r--common/resultprovider.h4
3 files changed, 13 insertions, 13 deletions
diff --git a/common/clientapi.cpp b/common/clientapi.cpp
index a98340c..8bb244c 100644
--- a/common/clientapi.cpp
+++ b/common/clientapi.cpp
@@ -5,26 +5,24 @@
5#include "resourcefacade.h" 5#include "resourcefacade.h"
6#include "log.h" 6#include "log.h"
7#include <QtConcurrent/QtConcurrentRun> 7#include <QtConcurrent/QtConcurrentRun>
8#define ASYNCINTHREAD
9#ifndef ASYNCINTHREAD
10#include <QTimer> 8#include <QTimer>
11#endif 9
10#define ASYNCINTHREAD
12 11
13namespace async 12namespace async
14{ 13{
15 void run(const std::function<void()> &runner) { 14 void run(const std::function<void()> &runner) {
16#ifndef ASYNCINTHREAD
17 auto timer = new QTimer(); 15 auto timer = new QTimer();
18 timer->setSingleShot(true); 16 timer->setSingleShot(true);
19 QObject::connect(timer, &QTimer::timeout, [runner, timer]() { 17 QObject::connect(timer, &QTimer::timeout, [runner, timer]() {
20 delete timer; 18 delete timer;
19#ifndef ASYNCINTHREAD
21 runner(); 20 runner();
22 });
23 timer->start(0);
24#else 21#else
25 //TODO use a job that runs in a thread? 22 QtConcurrent::run(runner);
26 QtConcurrent::run(runner);
27#endif 23#endif
24 });
25 timer->start(0);
28 }; 26 };
29} // namespace async 27} // namespace async
30 28
diff --git a/common/clientapi.h b/common/clientapi.h
index d860305..3ff8472 100644
--- a/common/clientapi.h
+++ b/common/clientapi.h
@@ -137,6 +137,10 @@ public:
137 //We must guarantee that the emitter is returned before the first result is emitted. 137 //We must guarantee that the emitter is returned before the first result is emitted.
138 //The result provider must be threadsafe. 138 //The result provider must be threadsafe.
139 async::run([query, resultSet](){ 139 async::run([query, resultSet](){
140 QEventLoop eventLoop;
141 resultSet->onDone([&eventLoop](){
142 eventLoop.quit();
143 });
140 // Query all resources and aggregate results 144 // Query all resources and aggregate results
141 KAsync::iterate(getResources(query.resources)) 145 KAsync::iterate(getResources(query.resources))
142 .template each<void, QByteArray>([query, resultSet](const QByteArray &resource, KAsync::Future<void> &future) { 146 .template each<void, QByteArray>([query, resultSet](const QByteArray &resource, KAsync::Future<void> &future) {
@@ -158,10 +162,6 @@ public:
158 162
159 //Keep the thread alive until the result is ready 163 //Keep the thread alive until the result is ready
160 if (!resultSet->isDone()) { 164 if (!resultSet->isDone()) {
161 QEventLoop eventLoop;
162 resultSet->onDone([&eventLoop](){
163 eventLoop.quit();
164 });
165 eventLoop.exec(); 165 eventLoop.exec();
166 } 166 }
167 }); 167 });
diff --git a/common/resultprovider.h b/common/resultprovider.h
index 4d985d9..a375815 100644
--- a/common/resultprovider.h
+++ b/common/resultprovider.h
@@ -100,7 +100,7 @@ public:
100 { 100 {
101 if (!mResultEmitter) { 101 if (!mResultEmitter) {
102 //We have to go over a separate var and return that, otherwise we'd delete the emitter immediately again 102 //We have to go over a separate var and return that, otherwise we'd delete the emitter immediately again
103 auto sharedPtr = QSharedPointer<ResultEmitter<T> >(new ResultEmitter<T>, [this](ResultEmitter<T> *emitter){ done(); delete emitter; }); 103 auto sharedPtr = QSharedPointer<ResultEmitter<T> >(new ResultEmitter<T>, [this](ResultEmitter<T> *emitter){ mThreadBoundary->callInMainThread([this]() {done();}); delete emitter; });
104 mResultEmitter = sharedPtr; 104 mResultEmitter = sharedPtr;
105 return sharedPtr; 105 return sharedPtr;
106 } 106 }
@@ -128,6 +128,7 @@ public:
128 128
129 void onDone(const std::function<void()> &callback) 129 void onDone(const std::function<void()> &callback)
130 { 130 {
131 mThreadBoundary = QSharedPointer<ThreadBoundary>::create();
131 mOnDoneCallback = callback; 132 mOnDoneCallback = callback;
132 } 133 }
133 134
@@ -151,6 +152,7 @@ private:
151 QSharedPointer<QObject> mQueryRunner; 152 QSharedPointer<QObject> mQueryRunner;
152 std::shared_ptr<void> mFacade; 153 std::shared_ptr<void> mFacade;
153 std::function<void()> mOnDoneCallback; 154 std::function<void()> mOnDoneCallback;
155 QSharedPointer<ThreadBoundary> mThreadBoundary;
154}; 156};
155 157
156/* 158/*