Skip to content

Commit

Permalink
[jsscripting] Rename library injection parameter & Improve docs (open…
Browse files Browse the repository at this point in the history
…hab#15547)

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

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored and pat-git023 committed Oct 13, 2023
1 parent 01fb07f commit ef76e29
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
18 changes: 15 additions & 3 deletions bundles/org.openhab.automation.jsscripting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,25 @@ to common openHAB functionality within rules including items, things, actions, l
## Configuration

This add-on includes by default the [openhab-js](https://github.com/openhab/openhab-js/) NPM library and exports its namespaces onto the global namespace.

This allows the use of `items`, `actions`, `cache` and other objects without the need to explicitly import them using `require()`.
This functionality can be disabled for users who prefer to manage their own imports via the add-on configuration options.

By default, the injection of the included [openhab-js](https://github.com/openhab/openhab-js/) NPM library is cached to improve performance and reduce memory usage.
If you want to use a different version of openhab-js (installed to the `node_modules` folder) than the included one, you need to disable the usage of the included library.
By default, the injection of the [openhab-js](https://github.com/openhab/openhab-js/) NPM library is cached (using a special mechanism instead of `require()`) to improve performance and reduce memory usage.

When configuring the add-on, you should ask yourself these questions:

1. Do I want to have the openhab-js namespaces automatically globally available (`injectionEnabled`)?
- Yes: "Use Built-In Variables" (default)
- No: "Do Not Use Built-In Variables", which will allow you to decide what to import and really speed up script loading, but you need to manually import the library, which actually will slow down script loading again.
2. Do I want to have a different version injected other than the included one (`injectionCachingEnabled`)?
- Yes: "Do Not Cache Library Injection" and install your version to the `$OPENHAB_CONF/automation/js/node_modules` folder, which will slow down script loading, because the injection is not cached.
- No: "Cache Library Injection" (default), which will speed up the initial loading of a script because the library's injection is cached.

Note that in case you disable caching or your code uses `require()` to import the library and there is no installation of the library found in the node_modules folder, the add-on will fallback to its included version.

![openHAB Rule Configuration](doc/settings.png)
In general, the first run of a script will take longer than the subsequent runs.
This is because on the first run both the globals (like `console`) and (if enabled) the library are injected into the script's context.

<!-- Paste the copied docs from openhab-js under this comment. Do NOT forget the table of contents. -->

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@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();

Expand All @@ -59,7 +59,7 @@ private static List<String> createScriptTypes() {
}

private boolean injectionEnabled = true;
private boolean useIncludedLibrary = true;
private boolean injectionCachingEnabled = true;

private final JSScriptServiceUtil jsScriptServiceUtil;
private final JSDependencyTracker jsDependencyTracker;
Expand Down Expand Up @@ -87,8 +87,8 @@ public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValu
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
Expand All @@ -99,6 +99,7 @@ public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValu
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand Down Expand Up @@ -279,7 +279,7 @@ protected void beforeInvocation() {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ef76e29

Please sign in to comment.