From eda5a562a0b07f00aee189cd7655df6e70168b6b Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Mon, 24 Oct 2022 23:40:44 +0200 Subject: [PATCH] [jsscripting] Name timers created by polyfills (#13576) * [jsscripting] Name timers created by polyfills Fixes #13478. Signed-off-by: Florian Hotze --- .../node_modules/@jsscripting-globals.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js b/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js index 1972efcff..e3b339914 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js +++ b/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js @@ -3,16 +3,16 @@ 'use strict'; // Append the script file name OR rule UID depending on which is available - const defaultLoggerName = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : ""); + const defaultIdentifier = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : ""); const System = Java.type('java.lang.System'); const ZonedDateTime = Java.type('java.time.ZonedDateTime'); const formatRegExp = /%[sdj%]/g; - function createLogger(name = defaultLoggerName) { + function createLogger(name = defaultIdentifier) { return Java.type("org.slf4j.LoggerFactory").getLogger(name); } - //user configurable + // User configurable let log = createLogger(); function stringify(value) { @@ -82,6 +82,8 @@ const counters = {}; const timers = {}; + // Polyfills for common NodeJS functions + const console = { 'assert': function (expression, message) { if (!expression) { @@ -159,7 +161,7 @@ } }, - //Allow user customizable logging names + // Allow user customizable logging names set loggerName(name) { log = createLogger(name); this._loggerName = name; @@ -173,6 +175,7 @@ function setTimeout(cb, delay) { const args = Array.prototype.slice.call(arguments, 2); return ThreadsafeTimers.createTimerWithArgument( + defaultIdentifier + '.setTimeout', ZonedDateTime.now().plusNanos(delay * 1000000), args, function (args) { @@ -191,6 +194,7 @@ const args = Array.prototype.slice.call(arguments, 2); const delayNanos = delay * 1000000 let timer = ThreadsafeTimers.createTimerWithArgument( + defaultIdentifier + '.setInterval', ZonedDateTime.now().plusNanos(delayNanos), args, function (args) { @@ -207,14 +211,14 @@ clearTimeout(timer); } - //Polyfil common functions onto the global object + // Polyfill common NodeJS functions onto the global object globalThis.console = console; globalThis.setTimeout = setTimeout; globalThis.clearTimeout = clearTimeout; globalThis.setInterval = setInterval; globalThis.clearInterval = clearInterval; - //Support legacy NodeJS libraries + // Support legacy NodeJS libraries globalThis.global = globalThis; globalThis.process = { env: { NODE_ENV: '' } }; })(this);