From b406460a8da3e332444e065f8560f6a87750cfbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Tue, 9 Feb 2021 10:38:27 +0800 Subject: [PATCH] add async call on main --- doric-Qt/doric/DoricContext.cpp | 2 ++ doric-Qt/doric/DoricContext.h | 2 ++ doric-Qt/doric/DoricNativeDriver.cpp | 1 + doric-Qt/doric/async/DoricAsyncCall.h | 15 +++++++++++++++ doric-Qt/doric/demo/DoricDemoBridge.cpp | 2 +- doric-Qt/doric/plugin/DoricShaderPlugin.cpp | 6 ++++++ doric-Qt/doric/utils/DoricConstant.cpp | 4 ++-- 7 files changed, 29 insertions(+), 3 deletions(-) diff --git a/doric-Qt/doric/DoricContext.cpp b/doric-Qt/doric/DoricContext.cpp index bfa6d0fd..f3dd32eb 100644 --- a/doric-Qt/doric/DoricContext.cpp +++ b/doric-Qt/doric/DoricContext.cpp @@ -59,6 +59,8 @@ DoricInterfaceDriver *DoricContext::getDriver() { return driver; } +DoricRootNode *DoricContext::getRootNode() { return mRootNode; } + QObject *DoricContext::obtainPlugin(QString name) { if (mPluginMap.keys().contains(name)) { return mPluginMap.value(name); diff --git a/doric-Qt/doric/DoricContext.h b/doric-Qt/doric/DoricContext.h index ddb33465..28b28270 100644 --- a/doric-Qt/doric/DoricContext.h +++ b/doric-Qt/doric/DoricContext.h @@ -30,6 +30,8 @@ public: DoricInterfaceDriver *getDriver(); + DoricRootNode *getRootNode(); + QObject *obtainPlugin(QString name); }; diff --git a/doric-Qt/doric/DoricNativeDriver.cpp b/doric-Qt/doric/DoricNativeDriver.cpp index 11d2f4cd..7869e029 100644 --- a/doric-Qt/doric/DoricNativeDriver.cpp +++ b/doric-Qt/doric/DoricNativeDriver.cpp @@ -23,6 +23,7 @@ DoricAsyncResult *DoricNativeDriver::asyncCall(std::function lambda, DoricThreadMode mode) { switch (mode) { case UI: + DoricAsyncCall::ensureRunInMain(lambda); break; case JS: DoricAsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool, lambda); diff --git a/doric-Qt/doric/async/DoricAsyncCall.h b/doric-Qt/doric/async/DoricAsyncCall.h index bc883ac0..7f13e882 100644 --- a/doric-Qt/doric/async/DoricAsyncCall.h +++ b/doric-Qt/doric/async/DoricAsyncCall.h @@ -13,6 +13,21 @@ public: QFuture::result_type> future = QtConcurrent::run(threadPool, lambda); } + + template + static void ensureRunInMain(Function &&function) { + struct Event : public QEvent { + using DecayedFunction = typename std::decay::type; + DecayedFunction decayedFunction; + Event(DecayedFunction &&decayedFunction) + : QEvent(QEvent::None), decayedFunction(std::move(decayedFunction)) {} + Event(const DecayedFunction &decayedFunction) + : QEvent(QEvent::None), decayedFunction(decayedFunction) {} + ~Event() { decayedFunction(); } + }; + QCoreApplication::postEvent(qApp, + new Event(std::forward(function))); + } }; #endif // ASYNC_CALL_H diff --git a/doric-Qt/doric/demo/DoricDemoBridge.cpp b/doric-Qt/doric/demo/DoricDemoBridge.cpp index 3d56d32f..09e24f80 100644 --- a/doric-Qt/doric/demo/DoricDemoBridge.cpp +++ b/doric-Qt/doric/demo/DoricDemoBridge.cpp @@ -9,7 +9,7 @@ DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} void DoricDemoBridge::navigate(QVariant route) { switch (route.toInt()) { case 0: - QString name = "Snake.js"; + QString name = "Snake.es5.js"; QString script = DoricUtils::readAssetFile("/doric/bundles", name); DoricPanel panel; diff --git a/doric-Qt/doric/plugin/DoricShaderPlugin.cpp b/doric-Qt/doric/plugin/DoricShaderPlugin.cpp index d599293d..cdea7f66 100644 --- a/doric-Qt/doric/plugin/DoricShaderPlugin.cpp +++ b/doric-Qt/doric/plugin/DoricShaderPlugin.cpp @@ -4,4 +4,10 @@ void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) { qDebug() << getContext(); + getContext()->getDriver()->asyncCall( + [this, jsValue] { + QString viewId = jsValue.property("id").toString(); + getContext()->getDriver(); + }, + DoricThreadMode::UI); } diff --git a/doric-Qt/doric/utils/DoricConstant.cpp b/doric-Qt/doric/utils/DoricConstant.cpp index aa615006..3128019f 100644 --- a/doric-Qt/doric/utils/DoricConstant.cpp +++ b/doric-Qt/doric/utils/DoricConstant.cpp @@ -1,7 +1,7 @@ #include "DoricConstant.h" -const QString DoricConstant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.js"; -const QString DoricConstant::DORIC_BUNDLE_LIB = "doric-lib.js"; +const QString DoricConstant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.es5.js"; +const QString DoricConstant::DORIC_BUNDLE_LIB = "doric-lib.es5.js"; const QString DoricConstant::DORIC_MODULE_LIB = "doric"; const QString DoricConstant::INJECT_ENVIRONMENT = "Environment";