From 9b0bee92d1397e28cae30127eec05a80783878c3 Mon Sep 17 00:00:00 2001 From: humbletim Date: Sat, 11 Feb 2017 06:21:11 -0500 Subject: [PATCH] add executeOnScriptThread --- libraries/script-engine/src/ScriptEngine.cpp | 29 ++++++++++++-------- libraries/script-engine/src/ScriptEngine.h | 1 + 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 683789b30a..f1ff4c4686 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -356,6 +356,16 @@ void ScriptEngine::runInThread() { workerThread->start(); } +void ScriptEngine::executeOnScriptThread(std::function function, bool blocking ) { + if (QThread::currentThread() != thread()) { + QMetaObject::invokeMethod(this, "executeOnScriptThread", blocking ? Qt::BlockingQueuedConnection : Qt::QueuedConnection, + Q_ARG(std::function, function)); + return; + } + + function(); +} + void ScriptEngine::waitTillDoneRunning() { auto workerThread = thread(); @@ -1490,18 +1500,13 @@ void ScriptEngine::updateEntityScriptStatus(const EntityItemID& entityID, const // since all of these operations can be asynch we will always do the actual work in the response handler // for the download void ScriptEngine::loadEntityScript(QWeakPointer theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { - - { - // set EntityScriptDetails.status to LOADING (over on theEngine's thread) - QObject threadPunter; - auto engine = theEngine.data(); - connect(&threadPunter, &QObject::destroyed, engine, [=]() { - EntityScriptDetails details = engine->_entityScripts[entityID]; - if (details.status == EntityScriptStatus::PENDING || details.status == EntityScriptStatus::UNLOADED) { - engine->updateEntityScriptStatus(entityID, EntityScriptStatus::LOADING, QThread::currentThread()->objectName()); - } - }); - } + auto engine = theEngine.data(); + engine->executeOnScriptThread([=]{ + EntityScriptDetails details = engine->_entityScripts[entityID]; + if (details.status == EntityScriptStatus::PENDING || details.status == EntityScriptStatus::UNLOADED) { + engine->updateEntityScriptStatus(entityID, EntityScriptStatus::LOADING, QThread::currentThread()->objectName()); + } + }); // NOTE: If the script content is not currently in the cache, the LAMBDA here will be called on the Main Thread // which means we're guaranteed that it's not the correct thread for the ScriptEngine. This means diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h index f970136ac4..a382258973 100644 --- a/libraries/script-engine/src/ScriptEngine.h +++ b/libraries/script-engine/src/ScriptEngine.h @@ -98,6 +98,7 @@ public: /// the current script contents and calling run(). Callers will likely want to register the script with external /// services before calling this. void runInThread(); + Q_INVOKABLE void executeOnScriptThread(std::function function, bool blocking = false); void runDebuggable();