don't block main thread for a script load

This commit is contained in:
Stephen Birarda 2014-12-02 10:30:23 -08:00
parent 36f716cd61
commit d7f168999d
4 changed files with 40 additions and 23 deletions

View file

@ -4013,26 +4013,23 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser
return _scriptEnginesHash[scriptURLString];
}
ScriptEngine* scriptEngine;
if (scriptFilename.isNull()) {
scriptEngine = new ScriptEngine(NO_SCRIPT, "", &_controllerScriptingInterface);
} else {
// start the script on a new thread...
scriptEngine = new ScriptEngine(scriptUrl, &_controllerScriptingInterface);
if (!scriptEngine->hasScript()) {
qDebug() << "Application::loadScript(), script failed to load...";
QMessageBox::warning(getWindow(), "Error Loading Script", scriptURLString + " failed to load.");
return NULL;
}
_scriptEnginesHash.insertMulti(scriptURLString, scriptEngine);
_runningScriptsWidget->setRunningScripts(getRunningScripts());
UserActivityLogger::getInstance().loadedScript(scriptURLString);
}
ScriptEngine* scriptEngine = new ScriptEngine(NO_SCRIPT, "", &_controllerScriptingInterface);
scriptEngine->setUserLoaded(isUserLoaded);
registerScriptEngineWithApplicationServices(scriptEngine);
if (scriptFilename.isNull()) {
// this had better be the script editor (we should de-couple so somebody who thinks they are loading a script
// doesn't just get an empty script engine)
// we can complete setup now since there isn't a script we have to load
registerScriptEngineWithApplicationServices(scriptEngine);
} else {
// connect to the appropriate signals of this script engine
connect(scriptEngine, &ScriptEngine::scriptLoaded, this, &Application::handleScriptEngineLoaded);
connect(scriptEngine, &ScriptEngine::scriptLoaded, this, &Application::handleScriptLoadError);
// get the script engine object to load the script at the designated script URL
scriptEngine->loadURL(scriptUrl);
}
// restore the main window's active state
if (activateMainWindow && !loadScriptFromEditor) {
@ -4043,6 +4040,22 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser
return scriptEngine;
}
void Application::handleScriptEngineLoaded(const QUrl& scriptURL) {
ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(sender());
_scriptEnginesHash.insertMulti(scriptURL.toString(), scriptEngine);
_runningScriptsWidget->setRunningScripts(getRunningScripts());
UserActivityLogger::getInstance().loadedScript(scriptURL.toString());
// register our application services and set it off on its own thread
registerScriptEngineWithApplicationServices(scriptEngine);
}
void Application::handleScriptLoadError(const QUrl& scriptURL) {
qDebug() << "Application::loadScript(), script failed to load...";
QMessageBox::warning(getWindow(), "Error Loading Script", scriptURL.toString() + " failed to load.");
}
void Application::scriptFinished(const QString& scriptName) {
const QString& scriptURLString = QUrl(scriptName).toString();
QHash<QString, ScriptEngine*>::iterator it = _scriptEnginesHash.find(scriptURLString);

View file

@ -393,6 +393,9 @@ private slots:
void timer();
void idle();
void aboutToQuit();
void handleScriptEngineLoaded(const QUrl& scriptURL);
void handleScriptLoadError(const QUrl& scriptURL);
void connectedToDomain(const QString& hostname);

View file

@ -177,10 +177,10 @@ void ScriptEngine::loadURL(const QUrl& scriptURL) {
qDebug() << "Loading file:" << fileName;
QTextStream in(&scriptFile);
_scriptContents = in.readAll();
emit scriptLoaded();
emit scriptLoaded(url);
} else {
qDebug() << "ERROR Loading file:" << fileName;
emit errorLoadingScript();
emit errorLoadingScript(url);
}
} else {
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
@ -195,9 +195,10 @@ void ScriptEngine::handleScriptDownload() {
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
_scriptContents = reply->readAll();
emit scriptLoaded(reply->url());
} else {
qDebug() << "ERROR Loading file:" << reply->url().toString();
emit errorLoadingScript();
emit errorLoadingScript(reply->url());
}
}

View file

@ -107,8 +107,8 @@ public slots:
void nodeKilled(SharedNodePointer node);
signals:
void scriptLoaded();
void errorLoadingScript();
void scriptLoaded(const QUrl& scriptURL);
void errorLoadingScript(const QUrl& scriptURL);
void update(float deltaTime);
void scriptEnding();
void finished(const QString& fileNameString);