From c59c91ec93c44586676437bf97f1fd4c18d34ffe Mon Sep 17 00:00:00 2001 From: Be Date: Sun, 14 Jun 2020 15:26:53 -0500 Subject: [PATCH] ControllerEngine: reload JS module when the file changes 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. --- src/controllers/engine/controllerengine.cpp | 10 ++++++++++ src/controllers/engine/controllerengine.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/controllers/engine/controllerengine.cpp b/src/controllers/engine/controllerengine.cpp index 890a48fd82f3..b7491ed2c745 100644 --- a/src/controllers/engine/controllerengine.cpp +++ b/src/controllers/engine/controllerengine.cpp @@ -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(); @@ -307,6 +316,7 @@ void ControllerEngine::reloadScripts() { qDebug() << "Re-initializing scripts"; initializeScripts(m_lastScriptFiles); + loadModule(m_moduleFileInfo); } void ControllerEngine::initializeScripts(const QList& scripts) { diff --git a/src/controllers/engine/controllerengine.h b/src/controllers/engine/controllerengine.h index 46bacbb5ca75..42ac2cba912e 100644 --- a/src/controllers/engine/controllerengine.h +++ b/src/controllers/engine/controllerengine.h @@ -171,6 +171,7 @@ class ControllerEngine : public QObject { QJSValue m_byteArrayToScriptValueJSFunction; // Filesystem watcher for script auto-reload QFileSystemWatcher m_scriptWatcher; + QFileInfo m_moduleFileInfo; QList m_lastScriptFiles; bool m_bTesting;