From 2645547f486c1a55ccba9fa7526fec0d26b8e1ff Mon Sep 17 00:00:00 2001 From: Kalila L Date: Thu, 27 Aug 2020 05:57:28 -0400 Subject: [PATCH] Add forceRedownload parameter to Script.require --- libraries/script-engine/src/ScriptEngine.cpp | 5 +++-- libraries/script-engine/src/ScriptEngine.h | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 3b2a122e71..641708e11b 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -4,6 +4,7 @@ // // Created by Brad Hefta-Gaub on 12/14/13. // Copyright 2013 High Fidelity, Inc. +// Copyright 2020 Vircadia contributors. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html @@ -1836,7 +1837,7 @@ QScriptValue ScriptEngine::instantiateModule(const QScriptValue& module, const Q } // CommonJS/Node.js like require/module support -QScriptValue ScriptEngine::require(const QString& moduleId) { +QScriptValue ScriptEngine::require(const QString& moduleId, bool forceRedownload) { qCDebug(scriptengine_module) << "ScriptEngine::require(" << moduleId.left(MAX_DEBUG_VALUE_LENGTH) << ")"; if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) { return unboundNullValue(); @@ -1875,7 +1876,7 @@ QScriptValue ScriptEngine::require(const QString& moduleId) { // `delete Script.require.cache[Script.require.resolve(moduleId)];` // cacheMeta is just used right now to tell deleted keys apart from undefined ones - bool invalidateCache = module.isUndefined() && cacheMeta.property(moduleId).isValid(); + bool invalidateCache = (module.isUndefined() && cacheMeta.property(moduleId).isValid()) || forceRedownload; // reset the cacheMeta record so invalidation won't apply next time, even if the module fails to load cacheMeta.setProperty(modulePath, QScriptValue()); diff --git a/libraries/script-engine/src/ScriptEngine.h b/libraries/script-engine/src/ScriptEngine.h index 15166d572f..4e855ed125 100644 --- a/libraries/script-engine/src/ScriptEngine.h +++ b/libraries/script-engine/src/ScriptEngine.h @@ -4,6 +4,7 @@ // // Created by Brad Hefta-Gaub on 12/14/13. // Copyright 2013 High Fidelity, Inc. +// Copyright 2020 Vircadia contributors. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html @@ -421,10 +422,11 @@ public: * @function Script.require * @param {string} module - The module to use. May be a JavaScript file, a JSON file, or the name of a system module such * as "appUi" (i.e., the "appUi.js" system module JavaScript file). + * @param {bool} [forceRedownload=false] - Invalidate the cache for this module and redownload it if necessary. * @returns {object|array} The value assigned to module.exports in the JavaScript file, or the value defined * in the JSON file. */ - Q_INVOKABLE QScriptValue require(const QString& moduleId); + Q_INVOKABLE QScriptValue require(const QString& moduleId, bool forceRedownload = false); /**jsdoc * @function Script.resetModuleCache