Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 2 files). Show all
Unified · Split
| @@ -356,6 +356,16 @@ void ScriptEngine::runInThread() { · 355 unmodified lines | ||
| 356 | 356 | workerThread->start(); |
| 357 | 357 | } |
| 358 | 358 | |
| 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 | + | |
| 359 | 369 | void ScriptEngine::waitTillDoneRunning() { |
| 360 | 370 | auto workerThread = thread(); |
| 361 | 371 | |
| @@ -1490,18 +1500,13 @@ void ScriptEngine::updateEntityScriptStatus(const EntityItemID& entityID, const · 1128 unmodified lines | ||
| 1490 | 1500 | // since all of these operations can be asynch we will always do the actual work in the response handler |
| 1491 | 1501 | // for the download |
| 1492 | 1502 | 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 | + }); | |
| 1505 | 1510 | |
| 1506 | 1511 | // NOTE: If the script content is not currently in the cache, the LAMBDA here will be called on the Main Thread |
| 1507 | 1512 | // which means we're guaranteed that it's not the correct thread for the ScriptEngine. This means |