add async call on main

This commit is contained in:
王劲鹏 2021-02-09 10:38:27 +08:00 committed by osborn
parent 60218cd8dd
commit b406460a8d
7 changed files with 29 additions and 3 deletions

View File

@ -59,6 +59,8 @@ DoricInterfaceDriver *DoricContext::getDriver() {
return driver; return driver;
} }
DoricRootNode *DoricContext::getRootNode() { return mRootNode; }
QObject *DoricContext::obtainPlugin(QString name) { QObject *DoricContext::obtainPlugin(QString name) {
if (mPluginMap.keys().contains(name)) { if (mPluginMap.keys().contains(name)) {
return mPluginMap.value(name); return mPluginMap.value(name);

View File

@ -30,6 +30,8 @@ public:
DoricInterfaceDriver *getDriver(); DoricInterfaceDriver *getDriver();
DoricRootNode *getRootNode();
QObject *obtainPlugin(QString name); QObject *obtainPlugin(QString name);
}; };

View File

@ -23,6 +23,7 @@ DoricAsyncResult *DoricNativeDriver::asyncCall(std::function<void()> lambda,
DoricThreadMode mode) { DoricThreadMode mode) {
switch (mode) { switch (mode) {
case UI: case UI:
DoricAsyncCall::ensureRunInMain(lambda);
break; break;
case JS: case JS:
DoricAsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool, lambda); DoricAsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool, lambda);

View File

@ -13,6 +13,21 @@ public:
QFuture<std::function<void()>::result_type> future = QFuture<std::function<void()>::result_type> future =
QtConcurrent::run(threadPool, lambda); QtConcurrent::run(threadPool, lambda);
} }
template <typename Function>
static void ensureRunInMain(Function &&function) {
struct Event : public QEvent {
using DecayedFunction = typename std::decay<Function>::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>(function)));
}
}; };
#endif // ASYNC_CALL_H #endif // ASYNC_CALL_H

View File

@ -9,7 +9,7 @@ DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {}
void DoricDemoBridge::navigate(QVariant route) { void DoricDemoBridge::navigate(QVariant route) {
switch (route.toInt()) { switch (route.toInt()) {
case 0: case 0:
QString name = "Snake.js"; QString name = "Snake.es5.js";
QString script = DoricUtils::readAssetFile("/doric/bundles", name); QString script = DoricUtils::readAssetFile("/doric/bundles", name);
DoricPanel panel; DoricPanel panel;

View File

@ -4,4 +4,10 @@
void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) { void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) {
qDebug() << getContext(); qDebug() << getContext();
getContext()->getDriver()->asyncCall(
[this, jsValue] {
QString viewId = jsValue.property("id").toString();
getContext()->getDriver();
},
DoricThreadMode::UI);
} }

View File

@ -1,7 +1,7 @@
#include "DoricConstant.h" #include "DoricConstant.h"
const QString DoricConstant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.js"; const QString DoricConstant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.es5.js";
const QString DoricConstant::DORIC_BUNDLE_LIB = "doric-lib.js"; const QString DoricConstant::DORIC_BUNDLE_LIB = "doric-lib.es5.js";
const QString DoricConstant::DORIC_MODULE_LIB = "doric"; const QString DoricConstant::DORIC_MODULE_LIB = "doric";
const QString DoricConstant::INJECT_ENVIRONMENT = "Environment"; const QString DoricConstant::INJECT_ENVIRONMENT = "Environment";