diff options
Diffstat (limited to 'common/asyncutils.h')
-rw-r--r-- | common/asyncutils.h | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/common/asyncutils.h b/common/asyncutils.h index 2cf010e..6cbcee8 100644 --- a/common/asyncutils.h +++ b/common/asyncutils.h | |||
@@ -25,17 +25,23 @@ | |||
25 | 25 | ||
26 | namespace async { | 26 | namespace async { |
27 | template <typename T> | 27 | template <typename T> |
28 | KAsync::Job<T> run(const std::function<T()> &f) | 28 | KAsync::Job<T> run(const std::function<T()> &f, bool runAsync = true) |
29 | { | 29 | { |
30 | return KAsync::start<T>([f](KAsync::Future<T> &future) { | 30 | if (runAsync) { |
31 | auto result = QtConcurrent::run(f); | 31 | return KAsync::start<T>([f](KAsync::Future<T> &future) { |
32 | auto watcher = new QFutureWatcher<T>; | 32 | auto result = QtConcurrent::run(f); |
33 | watcher->setFuture(result); | 33 | auto watcher = new QFutureWatcher<T>; |
34 | QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [&future, watcher]() { | 34 | watcher->setFuture(result); |
35 | future.setValue(watcher->future().result()); | 35 | QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [&future, watcher]() { |
36 | delete watcher; | 36 | future.setValue(watcher->future().result()); |
37 | future.setFinished(); | 37 | delete watcher; |
38 | future.setFinished(); | ||
39 | }); | ||
38 | }); | 40 | }); |
39 | }); | 41 | } else { |
42 | return KAsync::syncStart<T>([f]() { | ||
43 | return f(); | ||
44 | }); | ||
45 | } | ||
40 | } | 46 | } |
41 | } | 47 | } |