History

add executeOnScriptThread

humbletim committed 9 years ago

Commit 9b0bee91 parent 0f7652e2 files · +18 −12Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 2 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+17−12
@@ -356,6 +356,16 @@ void ScriptEngine::runInThread() { · 355 unmodified lines
356356 workerThread->start();
357357 }
358358
359+void ScriptEngine::executeOnScriptThread(std::function<void()> function, bool blocking ) {
360+ if (QThread::currentThread() != thread()) {
361+ QMetaObject::invokeMethod(this, "executeOnScriptThread", blocking ? Qt::BlockingQueuedConnection : Qt::QueuedConnection,
362+ Q_ARG(std::function<void()>, function));
363+ return;
364+ }
365+
366+ function();
367+}
368+
359369 void ScriptEngine::waitTillDoneRunning() {
360370 auto workerThread = thread();
361371
@@ -1490,18 +1500,13 @@ void ScriptEngine::updateEntityScriptStatus(const EntityItemID& entityID, const · 1128 unmodified lines
14901500 // since all of these operations can be asynch we will always do the actual work in the response handler
14911501 // for the download
14921502 void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
1493−
1494− {
1495− // set EntityScriptDetails.status to LOADING (over on theEngine's thread)
1496− QObject threadPunter;
1497− auto engine = theEngine.data();
1498− connect(&threadPunter, &QObject::destroyed, engine, [=]() {
1499− EntityScriptDetails details = engine->_entityScripts[entityID];
1500− if (details.status == EntityScriptStatus::PENDING || details.status == EntityScriptStatus::UNLOADED) {
1501− engine->updateEntityScriptStatus(entityID, EntityScriptStatus::LOADING, QThread::currentThread()->objectName());
1502− }
1503− });
1504− }
1503+ auto engine = theEngine.data();
1504+ engine->executeOnScriptThread([=]{
1505+ EntityScriptDetails details = engine->_entityScripts[entityID];
1506+ if (details.status == EntityScriptStatus::PENDING || details.status == EntityScriptStatus::UNLOADED) {
1507+ engine->updateEntityScriptStatus(entityID, EntityScriptStatus::LOADING, QThread::currentThread()->objectName());
1508+ }
1509+ });
15051510
15061511 // NOTE: If the script content is not currently in the cache, the LAMBDA here will be called on the Main Thread
15071512 // which means we're guaranteed that it's not the correct thread for the ScriptEngine. This means