Skip to content

Commit

Permalink
ControllerEngine: reload JS module when the file changes
Browse files Browse the repository at this point in the history
This only applies to the JS module loaded from the XML file. JS files
loaded from that via the JS `import` keyword are not reloaded unless the
module file loaded by the XML file is changed.
  • Loading branch information
Be-ing committed Jun 14, 2020
1 parent 39c64b5 commit d34c554
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/controllers/engine/controllerengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,20 @@ void ControllerEngine::uninitializeScriptEngine() {
}

void ControllerEngine::loadModule(QFileInfo moduleFileInfo) {
m_moduleFileInfo = moduleFileInfo;

QJSValue mod = m_pScriptEngine->importModule(moduleFileInfo.absoluteFilePath());
if (mod.isError()) {
showScriptExceptionDialog(mod);
return;
}

connect(&m_scriptWatcher,
&QFileSystemWatcher::fileChanged,
this,
&ControllerEngine::scriptHasChanged);
m_scriptWatcher.addPath(moduleFileInfo.absoluteFilePath());

QJSValue initFunction = mod.property("init");
if (initFunction.isCallable()) {
initFunction.call();
Expand Down Expand Up @@ -307,6 +316,7 @@ void ControllerEngine::reloadScripts() {

qDebug() << "Re-initializing scripts";
initializeScripts(m_lastScriptFiles);
loadModule(m_moduleFileInfo);
}

void ControllerEngine::initializeScripts(const QList<ControllerPreset::ScriptFileInfo>& scripts) {
Expand Down
1 change: 1 addition & 0 deletions src/controllers/engine/controllerengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class ControllerEngine : public QObject {
QJSValue m_byteArrayToScriptValueJSFunction;
// Filesystem watcher for script auto-reload
QFileSystemWatcher m_scriptWatcher;
QFileInfo m_moduleFileInfo;
QList<ControllerPreset::ScriptFileInfo> m_lastScriptFiles;

bool m_bTesting;
Expand Down

0 comments on commit d34c554

Please sign in to comment.