This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/example/doric/DoricNativeDriver.cpp

76 lines
2.3 KiB
C++
Raw Normal View History

2021-02-04 16:59:58 +08:00
#include <functional>
#include "DoricNativeDriver.h"
#include "async/DoricAsyncCall.h"
#include "utils/DoricConstant.h"
2021-05-12 11:08:40 +08:00
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::invokeContextEntityMethod(QString contextId, QString method,
QVariantList args) {
2021-02-04 16:59:58 +08:00
args.insert(0, QVariant(contextId));
args.insert(1, QVariant(method));
2021-05-12 11:08:40 +08:00
return invokeDoricMethod(DoricConstant::DORIC_CONTEXT_INVOKE, args);
2021-02-04 16:59:58 +08:00
}
2021-05-12 11:08:40 +08:00
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::invokeDoricMethod(QString method, QVariantList args) {
2021-05-25 10:45:45 +08:00
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
2021-05-12 11:08:40 +08:00
DoricAsyncCall::ensureRunInThreadPool(
2021-05-25 10:45:45 +08:00
&jsEngine.mJSThreadPool, [this, method, args, asyncResult] {
QString result = this->jsEngine.invokeDoricMethod(method, args);
asyncResult->setResult(result);
});
2021-05-12 11:08:40 +08:00
2021-05-25 10:45:45 +08:00
return asyncResult;
2021-02-04 16:59:58 +08:00
}
2021-05-12 11:08:40 +08:00
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::asyncCall(std::function<void()> lambda,
DoricThreadMode mode) {
2021-02-08 11:37:51 +08:00
switch (mode) {
case UI:
2021-05-25 10:45:45 +08:00
return DoricAsyncCall::ensureRunInMain(lambda);
2021-02-08 11:37:51 +08:00
break;
case JS:
2021-05-25 10:45:45 +08:00
return DoricAsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool,
lambda);
2021-02-08 11:37:51 +08:00
break;
}
2021-02-07 11:21:53 +08:00
return NULL;
}
2021-05-12 11:08:40 +08:00
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::createContext(QString contextId, QString script,
QString source) {
2021-05-25 10:45:45 +08:00
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
2021-02-04 16:59:58 +08:00
DoricAsyncCall::ensureRunInThreadPool(
2021-05-25 10:45:45 +08:00
&jsEngine.mJSThreadPool, [this, contextId, script, source, asyncResult] {
QString result =
this->jsEngine.prepareContext(contextId, script, source);
asyncResult->setResult(result);
2021-02-04 16:59:58 +08:00
});
2021-05-12 11:08:40 +08:00
2021-05-25 10:45:45 +08:00
return asyncResult;
2021-02-04 16:59:58 +08:00
}
2021-05-12 11:08:40 +08:00
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::destroyContext(QString contextId) {
2021-05-25 10:45:45 +08:00
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
2021-04-09 16:39:43 +08:00
DoricAsyncCall::ensureRunInThreadPool(
2021-05-25 10:45:45 +08:00
&jsEngine.mJSThreadPool, [this, contextId, asyncResult] {
QString result = this->jsEngine.destroyContext(contextId);
asyncResult->setResult(result);
});
2021-05-12 11:08:40 +08:00
2021-05-25 10:45:45 +08:00
return asyncResult;
2021-04-09 16:39:43 +08:00
}
2021-02-04 16:59:58 +08:00
DoricRegistry *DoricNativeDriver::getRegistry() {
return this->jsEngine.getRegistry();
}