add return value with async result

This commit is contained in:
王劲鹏
2021-05-25 10:45:45 +08:00
committed by osborn
parent 37f8898eac
commit da73ee5347
13 changed files with 95 additions and 94 deletions

View File

@@ -5,21 +5,43 @@
#include <QThreadPool>
#include <QtConcurrent/QtConcurrent>
#include "DoricAsyncResult.h"
class DoricAsyncCall {
public:
static void ensureRunInThreadPool(QThreadPool *threadPool,
std::function<void()> lambda) {
QFuture<std::function<void()>::result_type> future =
QtConcurrent::run(threadPool, lambda);
template <typename Function>
static std::shared_ptr<DoricAsyncResult>
ensureRunInThreadPool(QThreadPool *threadPool, Function &&function) {
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
Function lambda = std::forward<Function>(function);
QtConcurrent::run(threadPool, [lambda, asyncResult]() {
lambda();
// asyncResult->setResult(result);
});
return asyncResult;
}
template <typename Function>
static void ensureRunInMain(Function &&function,
QThread *thread = qApp->thread()) {
static std::shared_ptr<DoricAsyncResult>
ensureRunInMain(Function &&function, QThread *thread = qApp->thread()) {
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
auto *obj = QAbstractEventDispatcher::instance(thread);
Q_ASSERT(obj);
QMetaObject::invokeMethod(obj, std::forward<Function>(function));
Function lambda = std::forward<Function>(function);
QMetaObject::invokeMethod(obj, [lambda, asyncResult]() {
lambda();
// asyncResult->setResult(result);
});
return asyncResult;
}
};

View File

@@ -2,37 +2,12 @@
DoricAsyncResult::DoricAsyncResult() {}
DoricAsyncResult::DoricAsyncResult(QJSValue result) { this->result = result; }
void DoricAsyncResult::setResult(QString result) { this->result = result; }
void DoricAsyncResult::setResult(QJSValue result) {
this->result = result;
if (this->callback != NULL) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
void DoricAsyncResult::setError(QString exception) { this->result = exception; }
void DoricAsyncResult::setError(QJSValue exception) {
this->result = exception;
if (this->callback != NULL) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
bool DoricAsyncResult::hasResult() { return !result.equals(EMPTY); }
bool DoricAsyncResult::hasResult() { return !(result == EMPTY); }
QJSValue DoricAsyncResult::getResult() { return this->result; }
void DoricAsyncResult::setCallback(DoricCallback *callback) {
this->callback = callback;
if (this->result.isError()) {
this->callback->onError(result);
this->callback->onFinish();
} else if (!result.equals(EMPTY)) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
DoricSettableFuture *DoricAsyncResult::synchronous() { return NULL; }

View File

@@ -5,31 +5,29 @@
#include "DoricExport.h"
#include "DoricCallback.h"
#include "DoricSettableFuture.h"
static QJSValue EMPTY(QJSValue::NullValue);
static QString EMPTY("");
class DORIC_EXPORT DoricAsyncResult {
private:
QJSValue result = EMPTY;
DoricCallback *callback;
QString result = EMPTY;
public:
std::function<void()> resultCallback;
std::function<void()> exceptionCallback;
std::function<void()> finishCallback;
DoricAsyncResult();
DoricAsyncResult(QJSValue result);
void setResult(QString result);
void setResult(QJSValue result);
void setError(QJSValue exception);
void setError(QString exception);
bool hasResult();
QJSValue getResult();
void setCallback(DoricCallback *callback);
DoricSettableFuture *synchronous();
};

View File

@@ -1,15 +0,0 @@
#ifndef CALLBACK_H
#define CALLBACK_H
#include <QJSValue>
class DoricCallback {
public:
virtual void onResult(QJSValue result) = 0;
virtual void onError(QJSValue error) = 0;
virtual void onFinish() = 0;
};
#endif // CALLBACK_H