add executeOnScriptThread

This commit is contained in:
humbletim 2017-02-11 06:21:11 -05:00
parent 0f7652e173
commit 9b0bee92d1
2 changed files with 18 additions and 12 deletions

View file

@ -356,6 +356,16 @@ void ScriptEngine::runInThread() {
workerThread->start();
}
void ScriptEngine::executeOnScriptThread(std::function<void()> function, bool blocking ) {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "executeOnScriptThread", blocking ? Qt::BlockingQueuedConnection : Qt::QueuedConnection,
Q_ARG(std::function<void()>, 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<ScriptEngine> 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

View file

@ -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<void()> function, bool blocking = false);
void runDebuggable();