[jsscripting] Rename library injection parameter & Improve docs (#15547)

* [jsscripting] Rename parameter useIncludedLibrary to injectionCachingEnabled (#4)
* [jsscripting] Improve README for cached library injection
* Remove settings image

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze
2023-09-26 22:59:20 +02:00
committed by GitHub
parent afda8c5f47
commit 99f0512c73
6 changed files with 42 additions and 26 deletions

View File

@@ -45,7 +45,7 @@ import com.oracle.truffle.js.scriptengine.GraalJSEngineFactory;
@NonNullByDefault
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
private static final String CFG_USE_INCLUDED_LIBRARY = "useIncludedLibrary";
private static final String CFG_INJECTION_CACHING_ENABLED = "injectionCachingEnabled";
private static final GraalJSEngineFactory factory = new GraalJSEngineFactory();
@@ -59,7 +59,7 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
}
private boolean injectionEnabled = true;
private boolean useIncludedLibrary = true;
private boolean injectionCachingEnabled = true;
private final JSScriptServiceUtil jsScriptServiceUtil;
private final JSDependencyTracker jsDependencyTracker;
@@ -87,8 +87,8 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
if (!scriptTypes.contains(scriptType)) {
return null;
}
return new DebuggingGraalScriptEngine<>(new OpenhabGraalJSScriptEngine(injectionEnabled, useIncludedLibrary,
jsScriptServiceUtil, jsDependencyTracker));
return new DebuggingGraalScriptEngine<>(new OpenhabGraalJSScriptEngine(injectionEnabled,
injectionCachingEnabled, jsScriptServiceUtil, jsDependencyTracker));
}
@Override
@@ -99,6 +99,7 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
@Modified
protected void modified(Map<String, ?> config) {
this.injectionEnabled = ConfigParser.valueAsOrElse(config.get(CFG_INJECTION_ENABLED), Boolean.class, true);
this.useIncludedLibrary = ConfigParser.valueAsOrElse(config.get(CFG_USE_INCLUDED_LIBRARY), Boolean.class, true);
this.injectionCachingEnabled = ConfigParser.valueAsOrElse(config.get(CFG_INJECTION_CACHING_ENABLED),
Boolean.class, true);
}
}

View File

@@ -139,17 +139,17 @@ public class OpenhabGraalJSScriptEngine
private boolean initialized = false;
private final boolean injectionEnabled;
private final boolean useIncludedLibrary;
private final boolean injectionCachingEnabled;
/**
* Creates an implementation of ScriptEngine (& Invocable), wrapping the contained engine, that tracks the script
* lifecycle and provides hooks for scripts to do so too.
*/
public OpenhabGraalJSScriptEngine(boolean injectionEnabled, boolean useIncludedLibrary,
public OpenhabGraalJSScriptEngine(boolean injectionEnabled, boolean injectionCachingEnabled,
JSScriptServiceUtil jsScriptServiceUtil, JSDependencyTracker jsDependencyTracker) {
super(null); // delegate depends on fields not yet initialised, so we cannot set it immediately
this.injectionEnabled = injectionEnabled;
this.useIncludedLibrary = useIncludedLibrary;
this.injectionCachingEnabled = injectionCachingEnabled;
this.jsRuntimeFeatures = jsScriptServiceUtil.getJSRuntimeFeatures(lock);
LOGGER.debug("Initializing GraalJS script engine...");
@@ -279,7 +279,7 @@ public class OpenhabGraalJSScriptEngine
LOGGER.debug("Evaluating cached global script...");
delegate.getPolyglotContext().eval(GLOBAL_SOURCE);
if (this.injectionEnabled) {
if (this.useIncludedLibrary) {
if (this.injectionCachingEnabled) {
LOGGER.debug("Evaluating cached openhab-js injection...");
delegate.getPolyglotContext().eval(OPENHAB_JS_SOURCE);
} else {

View File

@@ -17,15 +17,15 @@
</options>
<default>true</default>
</parameter>
<parameter name="useIncludedLibrary" type="boolean" required="true">
<label>Use Included openHAB JavaScript Library</label>
<parameter name="injectionCachingEnabled" type="boolean" required="true">
<label>Cache openHAB JavaScript Library Injection</label>
<description><![CDATA[
Use the included openHAB JavaScript library for optimal performance.<br>
Disable this option to allow loading the library from the local user configuration directory "automation/js/node_modules". Using a user provided version of the library may increase script loading times, especially on less powerful systems.
Cache the openHAB JavaScript library injection for optimal performance.<br>
Disable this option to allow loading the library from the local user configuration directory "automation/js/node_modules". Disabling caching may increase script loading times, especially on less powerful systems.
]]></description>
<options>
<option value="true">Use Included Library</option>
<option value="false">Do Not Use Included Library</option>
<option value="true">Cache Library Injection</option>
<option value="false">Do Not Cache Library Injection</option>
</options>
<default>true</default>
</parameter>

View File

@@ -1,12 +1,15 @@
# add-on
addon.jsscripting.name = JavaScript Scripting
addon.jsscripting.description = This adds a JS (ECMAScript-2021) script engine.
# add-on
automation.config.jsscripting.injectionCachingEnabled.label = Cache openHAB JavaScript Library Injection
automation.config.jsscripting.injectionCachingEnabled.description = Cache the openHAB JavaScript library injection for optimal performance.<br>Disable this option to allow loading the library from the local user configuration directory "automation/js/node_modules". Disabling caching may increase script loading times, especially on less powerful systems.
automation.config.jsscripting.injectionCachingEnabled.option.true = Cache Library Injection
automation.config.jsscripting.injectionCachingEnabled.option.false = Do Not Cache Library Injection
automation.config.jsscripting.injectionEnabled.label = Use Built-in Global Variables
automation.config.jsscripting.injectionEnabled.description = Import all variables from the openHAB JavaScript library into all rules for common services like items, things, actions, log, etc... <br> If disabled, the openHAB JavaScript library can be imported manually using "<i>require('openhab')</i>"
automation.config.jsscripting.injectionEnabled.option.true = Use Built-in Variables
automation.config.jsscripting.injectionEnabled.option.false = Do Not Use Built-in Variables
automation.config.jsscripting.useIncludedLibrary.label = Use Included openHAB JavaScript Library
automation.config.jsscripting.useIncludedLibrary.description = Use the included openHAB JavaScript library for optimal performance.<br> Disable this option to allow loading the library from the local user configuration directory "automation/js/node_modules". Using a user provided version of the library may increase script loading times, especially on less powerful systems.
automation.config.jsscripting.useIncludedLibrary.option.true = Use Included Library
automation.config.jsscripting.useIncludedLibrary.option.false = Do Not Use Included Library
# service
service.automation.jsscripting.label = JavaScript Scripting