add return value with async result
This commit is contained in:
parent
37f8898eac
commit
da73ee5347
@ -14,11 +14,15 @@ DoricNativeDriver::invokeContextEntityMethod(QString contextId, QString method,
|
||||
|
||||
std::shared_ptr<DoricAsyncResult>
|
||||
DoricNativeDriver::invokeDoricMethod(QString method, QVariantList args) {
|
||||
std::shared_ptr<DoricAsyncResult> asyncResult =
|
||||
std::make_shared<DoricAsyncResult>();
|
||||
DoricAsyncCall::ensureRunInThreadPool(
|
||||
&jsEngine.mJSThreadPool,
|
||||
[this, method, args] { this->jsEngine.invokeDoricMethod(method, args); });
|
||||
&jsEngine.mJSThreadPool, [this, method, args, asyncResult] {
|
||||
QString result = this->jsEngine.invokeDoricMethod(method, args);
|
||||
asyncResult->setResult(result);
|
||||
});
|
||||
|
||||
return std::make_shared<DoricAsyncResult>();
|
||||
return asyncResult;
|
||||
}
|
||||
|
||||
std::shared_ptr<DoricAsyncResult>
|
||||
@ -26,10 +30,11 @@ DoricNativeDriver::asyncCall(std::function<void()> lambda,
|
||||
DoricThreadMode mode) {
|
||||
switch (mode) {
|
||||
case UI:
|
||||
DoricAsyncCall::ensureRunInMain(lambda);
|
||||
return DoricAsyncCall::ensureRunInMain(lambda);
|
||||
break;
|
||||
case JS:
|
||||
DoricAsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool, lambda);
|
||||
return DoricAsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool,
|
||||
lambda);
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
@ -38,21 +43,31 @@ DoricNativeDriver::asyncCall(std::function<void()> lambda,
|
||||
std::shared_ptr<DoricAsyncResult>
|
||||
DoricNativeDriver::createContext(QString contextId, QString script,
|
||||
QString source) {
|
||||
std::shared_ptr<DoricAsyncResult> asyncResult =
|
||||
std::make_shared<DoricAsyncResult>();
|
||||
|
||||
DoricAsyncCall::ensureRunInThreadPool(
|
||||
&jsEngine.mJSThreadPool, [this, contextId, script, source] {
|
||||
&jsEngine.mJSThreadPool, [this, contextId, script, source, asyncResult] {
|
||||
QString result =
|
||||
this->jsEngine.prepareContext(contextId, script, source);
|
||||
asyncResult->setResult(result);
|
||||
});
|
||||
|
||||
return std::make_shared<DoricAsyncResult>();
|
||||
return asyncResult;
|
||||
}
|
||||
|
||||
std::shared_ptr<DoricAsyncResult>
|
||||
DoricNativeDriver::destroyContext(QString contextId) {
|
||||
DoricAsyncCall::ensureRunInThreadPool(
|
||||
&jsEngine.mJSThreadPool,
|
||||
[this, contextId] { this->jsEngine.destroyContext(contextId); });
|
||||
std::shared_ptr<DoricAsyncResult> asyncResult =
|
||||
std::make_shared<DoricAsyncResult>();
|
||||
|
||||
return std::make_shared<DoricAsyncResult>();
|
||||
DoricAsyncCall::ensureRunInThreadPool(
|
||||
&jsEngine.mJSThreadPool, [this, contextId, asyncResult] {
|
||||
QString result = this->jsEngine.destroyContext(contextId);
|
||||
asyncResult->setResult(result);
|
||||
});
|
||||
|
||||
return asyncResult;
|
||||
}
|
||||
|
||||
DoricRegistry *DoricNativeDriver::getRegistry() {
|
||||
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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; }
|
||||
|
@ -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();
|
||||
};
|
||||
|
||||
|
@ -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
|
@ -101,7 +101,6 @@ HEADERS += \
|
||||
DoricRegistry.h \
|
||||
async/DoricAsyncCall.h \
|
||||
async/DoricAsyncResult.h \
|
||||
async/DoricCallback.h \
|
||||
async/DoricSettableFuture.h \
|
||||
engine/DoricBridgeExtension.h \
|
||||
engine/DoricInterfaceJSE.h \
|
||||
|
@ -13,7 +13,7 @@ public:
|
||||
virtual void injectGlobalJSFunction(QString name, QObject *function,
|
||||
QString property) = 0;
|
||||
|
||||
virtual void invokeObject(QString objectName, QString functionName,
|
||||
virtual QString invokeObject(QString objectName, QString functionName,
|
||||
QVariantList arguments) = 0;
|
||||
};
|
||||
|
||||
|
@ -96,18 +96,20 @@ DoricJSEngine::DoricJSEngine(QObject *parent) : QObject(parent) {
|
||||
}
|
||||
}
|
||||
|
||||
void DoricJSEngine::prepareContext(QString contextId, QString script,
|
||||
QString DoricJSEngine::prepareContext(QString contextId, QString script,
|
||||
QString source) {
|
||||
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
|
||||
return mJSE->loadJS(packageContextScript(contextId, script),
|
||||
"Context://" + source);
|
||||
}
|
||||
|
||||
void DoricJSEngine::destroyContext(QString contextId) {
|
||||
QString DoricJSEngine::destroyContext(QString contextId) {
|
||||
QString script =
|
||||
QString(DoricConstant::TEMPLATE_CONTEXT_DESTROY).replace("%s", contextId);
|
||||
mJSE->loadJS(script, "_Context://" + contextId);
|
||||
return mJSE->loadJS(script, "_Context://" + contextId);
|
||||
}
|
||||
|
||||
void DoricJSEngine::invokeDoricMethod(QString method, QVariantList arguments) {
|
||||
QString DoricJSEngine::invokeDoricMethod(QString method,
|
||||
QVariantList arguments) {
|
||||
return mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, method, arguments);
|
||||
}
|
||||
|
||||
|
@ -26,11 +26,11 @@ public:
|
||||
|
||||
~DoricJSEngine();
|
||||
|
||||
void prepareContext(QString contextId, QString script, QString source);
|
||||
QString prepareContext(QString contextId, QString script, QString source);
|
||||
|
||||
void destroyContext(QString contextId);
|
||||
QString destroyContext(QString contextId);
|
||||
|
||||
void invokeDoricMethod(QString method, QVariantList arguments);
|
||||
QString invokeDoricMethod(QString method, QVariantList arguments);
|
||||
|
||||
DoricRegistry *getRegistry();
|
||||
};
|
||||
|
@ -56,11 +56,13 @@ void DoricNativeJSE::injectGlobalJSFunction(QString name, QObject *function,
|
||||
}
|
||||
}
|
||||
|
||||
void DoricNativeJSE::invokeObject(QString objectName, QString functionName,
|
||||
QString DoricNativeJSE::invokeObject(QString objectName, QString functionName,
|
||||
QVariantList arguments) {
|
||||
if (mType == JSEType::V8) {
|
||||
v8Executor->invokeObject(objectName, functionName, arguments);
|
||||
return v8Executor->invokeObject(objectName, functionName, arguments);
|
||||
} else if (mType == JSEType::Native) {
|
||||
nativeExecutor->invokeObject(objectName, functionName, arguments);
|
||||
return nativeExecutor->invokeObject(objectName, functionName, arguments)
|
||||
.toString();
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ public:
|
||||
void injectGlobalJSFunction(QString name, QObject *function,
|
||||
QString property) override;
|
||||
|
||||
void invokeObject(QString objectName, QString functionName,
|
||||
QString invokeObject(QString objectName, QString functionName,
|
||||
QVariantList arguments) override;
|
||||
};
|
||||
|
||||
|
@ -72,7 +72,7 @@ void V8Executor::injectGlobalJSFunction(QString name, QObject *function,
|
||||
injectFunctions(nullptr, name.toUtf8().constData(), true);
|
||||
}
|
||||
|
||||
void V8Executor::invokeObject(QString objectName, QString functionName,
|
||||
QString V8Executor::invokeObject(QString objectName, QString functionName,
|
||||
QVariantList arguments) {
|
||||
std::string exception;
|
||||
v8::HandleScope handleScope(m_isolate);
|
||||
@ -86,6 +86,9 @@ void V8Executor::invokeObject(QString objectName, QString functionName,
|
||||
functionName.toUtf8().constData(),
|
||||
valueSize, js_values, &exception);
|
||||
delete[] js_values;
|
||||
|
||||
std::string result = JS2String(value);
|
||||
return QString::fromUtf8(result.c_str());
|
||||
}
|
||||
|
||||
// private segment
|
||||
|
@ -45,7 +45,7 @@ public:
|
||||
void injectGlobalJSFunction(QString name, QObject *function,
|
||||
QString property);
|
||||
|
||||
void invokeObject(QString objectName, QString functionName,
|
||||
QString invokeObject(QString objectName, QString functionName,
|
||||
QVariantList arguments);
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user